The difference between <context-param> and <init-param> in Web.xml is that <context-param> sets a parameter that is visible globally (ServletContext) in the application , <init-param> sets a parameter that is visible in the local (ServletRequest range) of the application.
<?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>testdemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>appversion</param-name>
<param-value>1.0</param-value>
</context-param>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test. Testservlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>yedward</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Package test;
Import java.io.IOException;
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 = 5222793251610509039L;
@Override
public void Init () throws Servletexception {
System.out.println (This.getinitparameter ("name"));
}
@Override
protected void Service (HttpServletRequest request, HttpServletResponse Resposne)
Throws Servletexception, IOException {
System.out.println (This.getinitparameter ("name"));
System.out.println (This.getservletcontext (). Getinitparameter ("appversion"));
System.out.println (Request.getservletcontext (). Getinitparameter ("appversion"));
}
}
The output from the above code is:
Yedward
Yedward
1.0
1.0
When you start a Web project, the container (such as Tomcat) goes to the configuration file Web.xml read two nodes:<listener></listener> and <context-param></ Context-param>, the container creates a servletcontext, all parts of the Web project will share this context, the container will <context-param></context-param> Convert to a key-value pair and give it to the ServletContext container to create a class instance in <listener></listener>, that is, to create a listener that will have contextinitialized in the monitor ( Servletcontextevent args) initialization method, obtained in this method:
ServletContext = Servletcontextevent.getservletcontext (); The value of the
Context-param = Servletcontext.getinitparameter (the "Context-param Key");
After you get the value of this context-param, you can do something about it. Note that this time your Web project is not fully started, this action will be earlier than all the servlet. In other words, this time, the operation of the key value in <context-param> will be executed before the Web project is fully launched.