Web. XML configuration Detailed Context-param
[HTML] view plaincopy
- <context-param>
- <param-name> Contextconfiglocation</param-name>
- < param-value>contextconfiglocationvalue></param-value>
- </context-param >
Role: This element 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 the value of the parameter name set
- 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 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")
Web. XML configuration Detailed Context-param