A Web project in the configuration <context-param> configuration function

Source: Internet
Author: User

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, 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.
Then it is possible to set up the database connection in <context-param> and initialize the database connection in the Listener class. 8. This listener is a class that is written by itself, in addition to the initialization method, It also has a destruction method. To release resources before closing the app. For example, the database connection is closed.
<!--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
*/contex T-param and Init-param distinguish
web.xml can define two parameters:
(1) parameters within the application range, stored in ServletContext, configured as follows in Web. xml:
           <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, as configured in Web. xml:
<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.

A Web project in the configuration <context-param> configuration function

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.