The role of <context-param>:
Configuration of Web. XML <context-param> Configuration function
1. When you start a Web project, the container (for example, Tomcat) reads its configuration file, Web. Read two nodes: <listener></listener> and <context-param></ Context-param>
2. Immediately after the container creates a servletcontext (context), all parts of the Web project will share this context.
3. The container converts <context-param></context-param> into a key-value pair and gives it to ServletContext.
4. The container creates a class instance in <listener></listener>, that is, the listener is created.
5. There will be a contextinitialized (Servletcontextevent args) initialization method in the listener, which gets servletcontext in this method = Servletcontextevent.getservletcontext ();
The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");
6. 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 yet. This action will be earlier than all servlets.
In other words, this time, your actions on key values in <context-param> will be executed before your Web project is fully booted.
7. For example, you might want to open the database before the project starts.
You can then set up the database connection in <context-param> and initialize the database connection in the Listener class.
8. This listener is a class of its own, in addition to the initialization method, it also has the method of destruction. Frees the resource before closing the app. For example, the database connection is closed.
Such as:
<!--load the spring configuration file--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/web-inf/applicationcontext.xml,/web-inf/action-servlet.xml,/web-
Inf/jason-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Another example:---> Custom context-param, and customize listener to get this information
<context-param>
<param-name>urlrewrite</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>cluster</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>servletmapping</param-name>
<param-value>*.bbscs</param-value>
</context-param>
<context-param>
<param-name>poststoragemode</param-name>
<param-value>1</param-value>
</context-param>
<listener>
<listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>
</listener>
public class Syslistener extends HttpServlet implements Servletcontextlistener {
Private static final Log logger = Logfactory.getlog (Syslistener.class);
public void contextdestroyed (Servletcontextevent sce) {
Used to operate when the container is closed.
}
Used when the container is open, the operation
public void contextinitialized (Servletcontextevent sce) {
String RootPath = Sce.getservletcontext (). Getrealpath ("/");
System.out.println ("-------------rootpath:" +rootpath);
if (RootPath! = null) {
RootPath = Rootpath.replaceall ("
\\\\", "/");
} else {
RootPath = "/";
}
if (!rootpath.endswith ("/")) {
RootPath = RootPath + "/";
}
Constant.rootpath = RootPath;
Logger.info ("Application Run Path:" + rootpath);
String Urlrewrtie = Sce.getservletcontext (). Getinitparameter ("Urlrewrite");
Boolean burlrewrtie = false;
if (urlrewrtie! = null) {
Burlrewrtie = Boolean.parseboolean (Urlrewrtie);
}
Constant.use_url_rewrite = Burlrewrtie;
Logger.info ("Use Urlrewrite:" + burlrewrtie);
Other slightly ....
}
}
/* Final Output
-------------rootpath:d:\tomcat_bbs\webapps\bbscs_8_0_3\
2009-06-09 21:51:46,526 [Com.laoer.bbscs.web.servlet.syslistener]-[info]
Application Run path:d:/tomcat_bbs/webapps/bbscs_8_0_3/
2009-06-09 21:51:46,526 [Com.laoer.bbscs.web.servlet.syslistener]-[info]
Use Urlrewrite:true
2009-06-09 21:51:46,526 [Com.laoer.bbscs.web.servlet.syslistener]-[info]
Use Cluster:false
2009-06-09 21:51:46,526 [Com.laoer.bbscs.web.servlet.syslistener]-[info]
SERVLET Mapping:*.bbscs
2009-06-09 21:51:46,573 [Com.laoer.bbscs.web.servlet.syslistener]-[info]
Post Storage mode:1
*/
Context-param and Init-param differences
There are two types of parameters that can be defined in Web. xml:
(1) Parameters within the application range, stored in the ServletContext, are configured in Web. Xml as follows:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
(2) The parameters within the Servlet range can only be obtained in the servlet's init () method, which is configured in Web. Xml as follows:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in Servlet init () </param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
In a servlet, you can use the code separately:
Package Com.wes.controller;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
public class Mainservlet extends HttpServlet ... {
Public Mainservlet () ... {
Super ();
}
public void Init () throws servletexception ... {
System.out.println ("The following two parameters param1 are stored in the servlet");
System.out.println (This.getinitparameter ("param1"));
System.out.println ("The following parameters are stored in the ServletContext");
System.out.println (Getservletcontext (). Getinitparameter ("Context/param"));
}
}
The first parameter in the servlet can be obtained by Getservletcontext (). Getinitparameter ("Context/param")
The second parameter can only be obtained by This.getinitparameter ("param1") in the servlet's init () method.