Differences and functions

Source: Internet
Author: User

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

<Context-param>:
<Context-param> configuration function in Web. xml configuration
1. when starting a web project, the container (such as Tomcat) will read its configuration file web. XML. read two nodes: <listener> </listener> and <context-param> </context-param>

2. Then, the container creates a servletcontext, and all parts of the web project share the context. 3. The Container converts <context-param> </context-param> to a key-Value Pair and submits it to servletcontext. 4. Create a class instance in the <listener> </listener> container, that is, create a listener. 5. The contextinitialized (servletcontextevent ARGs) initialization method will be available in the listener. In this method, obtain servletcontext = servletcontextevent. getservletcontext ();
Context-Param value = servletcontext. getinitparameter ("context-Param key"); 6. after obtaining the value of context-Param, you can perform some operations. note that your web project has not been fully started yet. this action will be earlier than all servlets.
In other words, the operations you perform on the key value in <context-param> will be executed before your web project is fully started. 7. example. you may want to open the database before the project starts.
Then, you can set the database connection method in <context-param> and initialize the database connection in the listener class. 8. this listener is a self-written class. In addition to the initialization method, it also has the destruction method. used to release resources before closing the application. for example, the database connection is closed. for example:
<! -- 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 is: ---> Custom context-Param and custom listener to get the 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)   {   // When the container is closed
} // When the container is enabled 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 );
Others ....}}
/* Final output
------------- Rootpath: D: \ tomcat_bbs \ webapps \ bbscs_8_0_3 \
21:51:46, 526 [COM. laoer. bbscs. Web. servlet. syslistener]-[info] application run path: D:/tomcat_bbs/webapps/bbscs_8_0_3/
21:51:46, 526 [COM. laoer. bbscs. Web. servlet. syslistener]-[info] Use urlrewrite: True
21:51:46, 526 [COM. laoer. bbscs. Web. servlet. syslistener]-[info] use cluster: false
21:51:46, 526 [COM. laoer. bbscs. Web. servlet. syslistener]-[info] Servlet Mapping: *. bbscs
21:51:46, 573 [COM. laoer. bbscs. Web. servlet. syslistener]-[info] Post storage mode: 1
*/ Differences between context-Param and init-Param
Two parameters can be defined in Web. xml:
(1) parameters within the application range are stored in servletcontext and configured in Web. XML as follows:
<Context-param>
<Param-Name> Context/Param </param-Name>
<Param-value> avalible during application </param-value>
</Context-param>
(2) servlet-specific parameters can only be obtained in the init () method of servlet, Configure the following 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> you can use Code Use:
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 servlet ");
System. Out. println (this. getinitparameter ("param1 "));
System. Out. println ("the following parameters are stored in servletcontext ");
System. Out. println (getservletcontext (). getinitparameter ("context/Param "));
}
} The first parameter can be obtained through getservletcontext (). getinitparameter ("context/Param") in servlet.
The second parameter can only be obtained through this. getinitparameter ("param1") in the servlet Init () method .------------------------------------

 
So there is nothing advanced
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> classpath: applicationcontext. xml </param-value>
</Context-param>
Instead of the applicationcontext. xml under/WEB-INF.
If not specified, applicationcontext. XML is loaded under/WEB-INF/by default.

The name contextconfiglocation is defined by spring, so it is not useful if you change it to another one.

For example, you have written a project or toolkit. For flexibility, some information is loaded by loading the configuration file. This configuration file can be stored in the XX/conf directory of the project, but some people are dissatisfied, saying that I want to place this configuration file at will, you also need to attach it to me. What do you do? You can tell him, okay, I'll change it for you. I defined a name named contextconfiglocation. You can write the value at will, and I will read the value in this fixed name to load the file.

That's it. Because you have defined the name and changed it to something else, you cannot read it.

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.