[Reprint] The difference between <context-param> and <init-param> of Web. xml

Source: Internet
Author: User

Original address: http://www.cnblogs.com/hzj-/articles/1689836.html

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 ("Context-param key"); 6. Once you have 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, at this point, 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.
So here you can set the connection of the database in <context-param>, initialize the connection of the database in the listening class. 8. This listener is a class that writes itself, in addition to the initialization method, it also has the method of destroying. 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);p ublic void contextdestroyed (servletcontextevent SCE) { //For operation when the container is closed
} //For operation when the container is openpublic 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) parameters within the Servlet range can only be obtained in the servlet's init () method,The configuration in Web. XML is 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> can be used in the servlet by code, respectively:
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.