1. Introduction to web.xml configuration nodes
(1) context-param
Format definition
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mybatis.xml</param-value></context-param>
effect:
- This element is used to declare context initialization parameters within the application scope (the entire WEB project).
- param-name Sets the parameter name of the context. Must be a unique name
- param-value sets the value of the parameter name, the example here is to specify the location of the spring configuration file.
(2) listener
Format definition
//listen-class Specifies the listening class, which inherits ServletContextListener. It contains the initialization method contextInitialized(ServletContextEvent event) and the destroy method contextDestoryed(ServletContextEvent event)<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
Function: This element is used to register a listener class. You can receive notifications about when an event occurs and what to use as a response. Event listeners are notified when establishing, modifying, and deleting sessions or servlet environments. Often used in conjunction with context-param.
(3) filter
Format definition
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param></filter><filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
Function: Used to specify a filter for WEB container. The filter can preprocess user requests before a request reaches the servlet, or process http responses when leaving the servlet; before executing the servlet, first execute the filter program and do some preprocessing work for it; modify the request and response according to the program needs; intercept the execution of the servlet after the servlet is called.
(4) servlet
- Format definition
//Configure Spring MVC and specify the servlet to process the request. There are two ways to: //1. The default address of the MVC configuration file is: /WEB-INF/${servletName}-servlet.xml//2. You can modify the location of the MVC configuration file through configuration. You need to specify the location of the MVC configuration file when configuring the DispatcherServlet. //The second method is used here <!-- Springmvc core controller--> <servlet> <servlet-name>dispatchServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatchServlet</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> effect:
- Create and return a complete html page containing dynamic content based on the nature of the client request;
- Create a portion of html pages (html snippets) that can be embedded into an existing html page;
- Read hidden data sent by the client;
- Read the display data sent by the client;
- Communication with other server resources (including databases and java applications);
2. web.xml loading process (steps):
- When starting a web project, the container (such as Tomcat and Apache) will read two nodes in its configuration file web.xml, context-param and listener.
- Immediately afterwards, the container will create a ServletContext (also known as Servlet context), which can be used by the entire WEB project within the application scope.
- The container converts <context-param> into key-value pairs and hands it over to ServletContext.
- The container creates a class instance in <listener>, that is, creates a listener. (Note: The class defined by the listener can be a custom class but must inherit the ServletContextListener).
- There will be a contextInitialized(ServletContextEvent args) initialization method in the listening method. In this method, we get: ServletContext = ServletContextEvent.getServletContext(); the value of context-param = ServletContext.getInitParameter("context-param key"); in this class, there must also be a contextDestroyed(ServletContextEvent event) destroy method. Used to release resources before closing the application, such as closing the database connection.
- After you get the value of this context-param, you can do some operations. Note that your WEB project has not been fully started at this time. This action will be earlier than all servlets. In other words, at this time, the operations you do with the key values in <context-param> will be executed before your WEB project is fully started.
- For example. You may want to open the database before the project starts. Then here you can set the database connection method in <context-param> and initialize the database connection in the listening class.
Supplementary knowledge: ServletContext is a global space for storing information. When the server starts, it exists. When the server is closed, it is released. request, one user can have multiple sessions, one user, and servletContext, all users share one. Therefore, in order to save space and improve efficiency, in ServletContext, it is safe to place necessary, important threads that all users need to share. For example, in a shopping website, users want to access the detailed information of products. If placed in the session domain, each user has to access the database, which is too inefficient; and in a ServletContext, once the server starts, it accesses the database and puts the product information into the database, so that all users can access the product information through the context.
3. Web.xml node loading order:
- The loading order of web.xml nodes has nothing to do with the order of their position in web.xml, that is, the < filter > will not be loaded first just because < filter > is written before < context-param >.
- As mentioned above, <context-param> is used to provide key-value pairs to ServletContext, i.e. context information of the application. The listener, servlet and other nodes will use these context information during the initialization process, so we finally concluded that the loading order of the web.xml node should be: context-param->listener->filter->servlet.
- For a certain type of configuration node, the position sequence is required. Take servlet as an example. The configuration node related to servlet is servlet-mapping. For servlets and servlet-mapping with the same configuration section servlet-name, servlet-mapping must be defined after servlet. Otherwise, when parsing to servlet-mapping, its servlet-name has not been defined yet. When each servlet is initialized at the start of the web container, it is initialized in the order in which the servlet configuration section appears.
- Final conclusion: The loading order of web.xml is: [context-param -> listener -> filter -> servlet -> spring] , and the order of actual program calls between nodes of the same type is called according to the corresponding mapping order.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.