Web Project Initialization process:
- When you start a Web project, the container (such as Tomcat) reads the two nodes <listener> and <contex-param> in the. 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, the listener is created (note: The class defined by listener can be a custom class but must inherit Servletcontextlistener).
- There is a contextinitialized (Servletcontextevent event) initialization method in the listener class, which can be passed Event.getservletcontext () in this method. Getinitparameter ("Contextconfiglocation") to get the value Context-param set. There must also be a contextdestroyed (Servletcontextevent event) Destruction method in this class. To release resources before closing the app, such as closing the database connection.
- 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.
By the initialization process above, it is known that the loading process of the container for Web. XML is Context-param->listener->fileter->servlet
The context-param label format is as follows:
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>contextconfiglocationvalue</param-value>
</context-param>
Context-param is used to declare 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 parameter name
Ways to get Param-value://contextconfiglocation to param-name
On the page
${initparam.contextconfiglocation}
In the servlet
String Paramvalue=getservletcontext (). Getinitparameter ("Contextconfiglocation");
Transferred from: http://blog.csdn.net/liaoxiaohua1981/article/details/6759206
The Context-param of the Web. XML tag in a Java project