Configuration of Web. XML <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 thereafter, the container creates a ServletContext (context) in which all parts of the Web project will share the context. 3. The container will be <context-param></ The context-param> is converted to a key-value pair and handed to servletcontext.4. Container to create a class instance in <listener></listener> That is, the listener is created. 5. There is 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.
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>:---> 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 Lo G logger = Logfactory.getlog (syslistener.class);p ublic void contextdestroyed (Servletcontextevent sce) {//For operation when the container is closed
}//is used to operate public void contextinitialized (Servletcontextevent SCE) when the container is open {
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, 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> 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.

Configuration of Web. XML <context-param> Configuration function

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.