web.xml loading order of process

order:

  Overall, the loading sequence is web.xml <context-param> -> <listener> -> <filter> -> <the servlet>. Wherein, if the web.xml appears identical elements, in accordance with the order of appearance in the configuration file to load.

process:

  Briefly about, loading the web.xml. When we start a project WEB container, including container (JBoss, Tomcat, etc.). First, go read the web.xml configuration file configuration, when this step is complete and without error, the project is to get it to start up.

  Start WEB project when the container is first to read the two nodes web.xml configuration file: <context-param> </ context-param> and <listener> </ listener>

  Then, create a container ServletContext (application), all part of this web project will be shared in this context. Container <context-param> </ context -param> the name as the key, value as the value, converted to the key-value pairs stored ServletContext.

  Container creates a class instance <listener> </ listener> in, the configuration of a class-based paths <listener-class> to create a listener, the listener will have the initialization process starts when the Web application, the system calls the Listener which  the contextInitialized ( ServletContextEvent args), obtained in this method:

  ServletContext application =ServletContextEvent.getServletContext();

  value of context-param application.getInitParameter ( "context-param key ");

  After obtaining the value of context-param, you can do some of the operations.

  Example: You might want to start the project before the database is opened, this will be the setting database connections <context-param> in (driver, url, the User, password), initialize the database connection with the listener class. This monitor is to write a class, in addition to the initialization method, it also destroyed a method for releasing resources before closing the application. For example : say close the database connection, in which case, call contextDestroyed (ServletContextEvent args), shut down a Web application, the system calls Listener of the method.

  Subsequently, the container is read <filter> </ filter>, instantiating the class according to the specified path filter .

  These are the time WEB project has not fully started up work has been completed. If the system has a Servlet, the Servlet is initiated when the first request is instantiated , and the container generally will not be destroyed, it can be a service request to a plurality of users. So, Servlet initialization better than the above-mentioned to a few late.

Therefore, according to the loading process can be seen, the load order is web.xml <context-param> -> <listener> -> <filter> -> <the servlet>. Wherein, if the web.xml appears identical elements, in accordance with the order of appearance in the configuration file to load.

Drink from the source: https://www.cnblogs.com/shoshana-kong/p/10682662.html

Guess you like

Origin www.cnblogs.com/longchuqianyuan/p/11550152.html