package com.pas.web; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.omg.CORBA.Environment; public class ServletConfigTest extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding(this.getServletConfig().getInitParameter("encoding")); response.getWriter().println("你好世界aaaa"); Enumeration<String> en= this.getServletConfig().getInitParameterNames(); String lineSeparator = System.getProperty("line.separator", "/n"); while(en.hasMoreElements()) { response.getWriter().println(en.nextElement()+lineSeparator); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
Web.XML java
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>com.pas.web.ServletConfigTest</servlet-class> <init-param> <param-name>encoding</param-name> <param-value>gb2312</param-value> </init-param> </servlet>
<context-param> <param-name>context_param</param-name> <param-value>value3</param-value> </context-param>
此類設置對應web應用全局訪問: web
this.getServletContext().getInitParameter("context_param")