Servlet2.5是基于 xml 配置的,所以在xml中配置。 Servlet3.0开始支持 注解 配置。
配置分两种情况:在当前 Servlet有效 和在 整个web容器 中有效。
对于整个web容器有效的情况下,无论是2.5还是3.0(及以上)都必须在xml中配置。
配置整个web容器有效时使用 <context-param></context-param> 配置当前Servlet有效时在对应的<servlet></servlet>使用<init-param></init-param>
配置文件如下:
<?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_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>globalParamName</param-name> <param-value>globalParamValue</param-value> </context-param> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>TestServlet</servlet-class> <init-param> <param-name>servletParamName</param-name> <param-value>servletParamValue</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </web-app>读取:
public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public TestServlet() { } @Override public void init() throws ServletException { super.init(); String value = super.getInitParameter("servletParamName"); System.out.println("当前Servlet的servletParam值为"+value); ServletContext sc = super.getServletContext(); String gValue = sc.getInitParameter("globalParamName"); System.out.println("当前Web容器的gobalValue为"+gValue); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }name和value分别对应
<param-name> <param-value> @WebServlet(value="/TestServlet",loadOnStartup=1,initParams= {@WebInitParam(name="servletnameParam30",value="servletValue30")}) public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TestServlet() { // TODO Auto-generated constructor stub } @Override public void init() throws ServletException { super.init(); String value = super.getInitParameter("servletnameParam30"); System.out.println("当前Servlet的servletParam30值为"+value); ServletContext sc = super.getServletContext(); String gValue = sc.getInitParameter("globalParamName"); System.out.println("当前Web容器的gobalValue为"+gValue); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }