Listener, filter, servlet load order in Web. xml

Source: Internet
Author: User

I. Overview

1. When starting a Web project, the Web container reads its config file, XML, and reads <listener> and <context-param> two nodes.

2, emergency, allow to create a ServletContext (servlet context), all parts of this Web project will share this context.

3, the container will be <context-param> converted to a key value pair, and handed to ServletContext.

4. Create a listener for the class instance in the <listener> container.

two , Load-on-startup

The Load-on-startup element specifies the order in which the servlet is loaded when the Web application is launched, and its value must be an integer. If its value is a negative integer or the element does not exist, the container will load the servlet when the servlet is called. If the value is a positive integer or 0, the container loads and initializes the servlet when configured, and the container must ensure that the value is loaded first. If the values are equal, the container can automatically select who to load first.

In the configuration of the servlet, the meaning of,<load-on-startup>5</load-on-startup> is:

Flags whether the container loads the servlet when it is started.

A value of 0 or greater than 0 o'clock indicates that the container loads the servlet when the app is started;

When it is a negative number or is not specified, it indicates that the container is loaded only when the servlet is selected.

The lower the value of a positive number, the higher the priority to start the servlet.

three , load order

The first thing to be sure is that the order of loading is independent of their order in the Web. xml file. That is, the filter is not loaded before the filter is written in front of the listener. The final conclusion is: ServletContext, listener, filter, servlet

There is also a configuration section: Context-param, which is used to provide ServletContext with key-value pairs, which are application context information. Our listener, filter, and so on are initialized with the information in these contexts, so should the Context-param configuration section be written before the Listener configuration section? In fact, the Context-param configuration section can be written anywhere, so the real load order is: Context-param, listener, filter, servlet

For a class of configuration sections, it is related to the order in which they appear. In the case of filter, it is possible to define multiple filter in Web. XML, a configuration section related to filter is filter-mapping, and it is important to note that for filter and filter-mappin with the same filter-name In the case of the G configuration section, the filter-mapping must appear after the filter, or the corresponding filter-name is undefined when parsing to filter-mapping. When each filter is initialized at the start of the Web container, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, the filter interception resource is invoked in the order in which the Filter-mapping configuration section appears in sequence D The Ofilter () method.

The servlet is similar to filter and is not mentioned here.

From this, it can be seen that the loading order of Web. XML is: ServletContext, Context-param-A, listener-a servlet, The order of the actual program calls between the same types is called according to the Order of the corresponding mapping.

Iv. differences between Context-param and Init-param

There are two types of parameters that can be defined in Web. xml:

(1) application in the range of parameters, stored in the ServletContext, in Web. XML is configured as follows:
<context-param>
           < Param-name>context/param</param-name>
          < param-value>avalible during application</ Param-value>
</context-param>

(2) The parameters within the Servlet range can only be obtained in the servlet's init () method, which is configured in Web. Xml as follows:
<servlet>
<servlet-name>MainServlet</servlet-name>
< servlet-class>com.wes.controller.mainservlet</servlet-class>
<init-param>
< param-name>param1</param-name>
<param-value>avalible in Servlet init () </param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

containers (such as Tomcat) read the order of the two parameters : First application, then the servlet.

how to read two parameters in a servlet :

The parameters in the application can be obtained by Getservletcontext (). Getinitparameter ("Context/param") within the servlet.

Parameters in the servlet can only be obtained by This.getinitparameter ("param1") in the servlet's init () method

The code is as follows

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.wes.controller; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; public class MainServlet extends HttpServlet ...{     public MainServlet() ...{         super();      }     public void init() throws ServletException ...{          System.out.println("下面的两个参数param1是在servlet中存放的");          System.out.println(this.getInitParameter("param1"));          System.out.println("下面的参数是存放在servletcontext中的");         System.out.println(getServletContext().getInitParameter("context/param"));       } }

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.