标签:
可以进行属性遍历;
package com.stono.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public TestServlet() { System.out.println("com.stono.servlet.TestServlet constructor called"); } @Override public void init() throws ServletException { System.out.println("com.stono.servlet.TestServlet.init() called"); } @Override public void destroy() { System.out.println("com.stono.servlet.TestServlet.destroy() called"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = req.getServletContext(); Enumeration<String> attributeNames = context.getAttributeNames(); int i = 0; while (attributeNames.hasMoreElements()) { String nextElement = attributeNames.nextElement(); System.out.print(++i + ":" + nextElement + ":"); Object attribute = context.getAttribute(nextElement); System.out.println(attribute); } ServletConfig config2 = getServletConfig(); String name = config2.getServletName(); System.out.println(name); Enumeration<String> names = config2.getInitParameterNames(); i = 0; while (names.hasMoreElements()) { String nextElement = names.nextElement(); System.out.print(++i + ":" + nextElement + ":"); String initParameter = config2.getInitParameter(nextElement); System.out.println(initParameter); } RequestDispatcher dispatcher = req.getRequestDispatcher("test2.do"); dispatcher.forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>TestBootWeb</display-name> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>com.stono.servlet.TestServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>123</param-value> </init-param> </servlet> <servlet> <servlet-name>TestServlet2</servlet-name> <servlet-class>com.stono.servlet.TestServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/test.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>TestServlet2</servlet-name> <url-pattern>/test2.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
ServletConfig、ServletContext属性遍历
标签:
原文地址:http://www.cnblogs.com/stono/p/4831174.html