Servlet初始化参数(Servlet2.5,3.0)

一、使用Servlet2.5初始化参数

Servlet2.5是基于 xml 配置的,所以在xml中配置。
Servlet3.0开始支持 注解 配置。

配置分两种情况:在当前 Servlet有效 和在 整个web容器 中有效。

对于整个web容器有效的情况下,无论是2.5还是3.0(及以上)都必须在xml中配置。

配置整个web容器有效时使用
配置当前Servlet有效时在对应的使用

配置文件如下:


<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>globalParamNameparam-name>
    	<param-value>globalParamValueparam-value>
  context-param>
  
  
  <servlet>
    <description>description>
    <display-name>TestServletdisplay-name>
    <servlet-name>TestServletservlet-name>
    <servlet-class>TestServletservlet-class>
     <init-param>
    	<param-name>servletParamNameparam-name>
    	<param-value>servletParamValueparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  
  <servlet-mapping>
    <servlet-name>TestServletservlet-name>
    <url-pattern>/TestServleturl-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);
	}

}

二、使用Servlet3.0初始化参数

initParams= {@WebInitParam(name="servletnameParam30",value="servletValue30")}

name和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);
	}

}


你可能感兴趣的:(Java,EE)