Differences and functions in Web. xml

Source: Internet
Author: User
<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 the class instance in servletcontext.4. the container creates a <listener> </listener> to create a listener. 5. the contextinitialized (servletcontextevent ARGs) initialization method is available in the listener. In this method, the servletcontext = servletcontextevent is obtained. 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/Act Ion-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: ---> customize context-Param, and customize listener to obtain 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) {// used for operations when the container is closed

} // Used to operate 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

*/Difference 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. 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> you can use the following code in the servlet:

Package com. Wes. Controller; imp ORT javax. servlet. servletexception;

IMP ORT 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.


Http://www.cnblogs.com/hzj-/articles/1689836.html

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.