Transferred from: http://blog.csdn.net/liaoxiaohua1981/article/details/6759206
[HTML]View Plaincopy
- < context-param >
- <Param-name>contextconfiglocation</Param-name>
- <Param-value>Contextconfiglocationvalue></Param-value>
- </ context-param >
Role: This element is used to declare the context initialization parameters within the scope of the application (the entire Web project).
Param-name sets the parameter name of the context. Must be a unique name
Param-value set the value of the name of the parameter
- when you start a Web project, the container (for example, Tomcat) Read the two nodes <listener> and <contex-param> in the Web. XML configuration file.
- The container then creates a ServletContext (context) that can be used by the entire Web project within the scope of the application.
- The container then converts the read to <context-param> into a key-value pair and gives it to ServletContext. The
- container creates a class instance in <listener></listener>, That is to create a listener (note: The class defined by listener can be a class of its own definition but must inherit Servletcontextlistener). The
- has a contextinitialized (Servletcontextevent event) initialization method in the listening class, which can be passed Event.getservletcontext (). Getinitparameter ("Contextconfiglocation") to get the value Context-param set. There must also be a contextdestroyed (Servletcontextevent event) Destruction method in this class. Frees resources before closing the app, such as the shutdown of a database connection.
- After you get the value of this context-param, you can do something about it. Note that your Web project is not fully started at this time. This will be earlier than the entire servlet.
By the above initialization process, it is known that the container loading process for Web. XML is context-param >> listener >> fileter >> servlet
- On the page
${initparam.contextconfiglocation}
- In the servlet
String paramvalue=getservletcontext (). Getinitparameter ("contextconfiglocation")
Disclaimer: This content is collated
"Web. XML configuration specific explanation of the Context-param"