Context-param,listener,filter,servlet loading sequence of Web. XML and its surrounding

Source: Internet
Author: User

Take spring as an example to see the load order:

Spring loading can be implemented using Servletcontextlistener or Load-on-startup Servlet implementations, but for example, the filter needs to use the bean, but the load order is: loading the filter after the spring , the bean in the initialization operation in filter is NULL, so if you want to use the bean in the filter, you can change the load of spring to listener by listener>filter>servlet the load order.

Using Servletcontextlistener to achieve:

 
    1. <SERVLET>&NBSP;&NBSP;
    2.         <servlet-name> context</servlet-narne>  
    3.          <servlet-class>  
    4.         org.springframework.web.context.contextloaderservlet   
    5.          </servlet-class>  
    6.          <load-on-startup>1</load-on-startup>  
    7. </SERVLET>&NBSP;&NBSP;

Implemented with the Load-on-startup Servlet:

    1. <listener>
    2. <listener-class>
    3. Org.springframework.web.context.ContextLoaderListener
    4. </listener-class>
    5. </listener>

Web. XML loading process (steps):


1. When you start a Web project, the container (for example, Tomcat) reads its configuration file Web. Read TWO nodes:
<listener></listener> and <context-param></context-param>

2. Immediately after the container creates a servletcontext (context), all parts of the Web project will share this context.

3. The container converts <context-param></context-param> into a key-value pair and gives it to ServletContext.

4. The container creates a class instance in <listener></listener>, that is, the listener is created.

5. There is a contextinitialized (Servletcontextevent args) initialization method in the listener, which is obtained in this method:
ServletContext = Servletcontextevent.getservletcontext ();
The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");

6. 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.
In other words, this time, your actions on key values in <context-param> will be executed before your Web project is fully booted.

7. For example, you might want to open the database before the project starts.
You can then set up the database connection in <context-param> and initialize the database connection in the Listener class.

8. This listener is a class of its own, in addition to the initialization method, it also has the method of destruction. Frees the resource before closing the app. For example, the database connection is closed.

The first thing to be sure is that the order of loading is not related to the order in which they appear in Web. XML, that is, the fiter is not loaded before the filter is written in listener, and so on.

In the case of <context-param>, <listener>, <filter>, <servlet> four nodes, the load order when starting the server is Context-param > Listener > Filter > servlet.

The first is <context-param> When you start a Web project, the Web container reads its config file, XML, reads <listener> and <context-param> two nodes, which are used to ServletContext provides key-value pairs, which are application context information. For example, our listener, filter, etc. will use the information in these contexts when initializing.

For example, in the listener there will be a contextinitialized (Servletcontextevent args) initialization method, which is obtained in this method:

ServletContext = Servletcontextevent.getservletcontext ();

The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");

ServletContext = Servletcontextevent.getservletcontext ();

The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");

It consists of two sub-elements: Param-name,param-value,

The former is used to set the name of the context, which is used to set its value. For example


 
    1. <context-param>  
    2.        <param-name> contextconfiglocation</param-name>  
    3.        <param-value>  
    4.             classpath*:/applicationcontext.xml  
    5.            classpath*:/ applicationcontext-security.xml  
    6.         </param-value>  
    7. </CONTEXT-PARAM>&NBSP;&NBSP;

when Param-value has multiple values, it can be separated by a space or a comma, or wildcard characters .

followed by listener, the listener element is used to define the listener interface, and its primary child element is < listener-class>

< Listen-class>listener class Name </listener-class> defines the class name of the listener. For example: Com.foo.hello

Example:


 
    1. <listener>
    2. <listener-class>
    3. Org.springframework.web.context.ContextLoaderListener
    4. </listener-class>
    5. </listener>

And then the filter.

The filter element is used to declare the relevant settings for the filter. The filter element includes, in addition to the child elements described below, < icon>,< display-name>,< description>,< Init-param>, for the same purpose.

< Filter-name>filter name </filter-name> defines the name of the filter.

< Filter-class>filter class Name </filter-class> defines the class name of the filter. For example: Com.foo.hello

< filter-mapping>

The two main child elements of the filter-mapping element Filter-name and Url-pattern. Used to define the URL to which the filter corresponds.

< Filter-name>filter name </filter-name> defines the name of the filter.

< url-pattern>url< the corresponding Rul of/url-pattern>filter.

For example:< url-pattern>/filter/hello</url-pattern>

< Servlet-name>servlet name < servlet-name> defines the name of the servlet.

< dispatcher>request| include| forward| error</disaptcher> Set the filter corresponding request method, there are rquest,include,forwar,error four kinds, the default is request.

Example:


 
  1. <filter>
  2. <filter-name>
  3. Hibernateopensessioninviewfilter
  4. </filter-name>
  5. <filter-class>
  6. Org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  7. </filter-class>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>
  11. Hibernateopensessioninviewfilter
  12. </filter-name>
  13. <url-pattern>*.action</url-pattern>
  14. <dispatcher>REQUEST</dispatcher>
  15. <dispatcher>FORWARD</dispatcher>
  16. </filter-mapping>

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 when the Web container starts, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, the filter intercept resource is in accordance with the filter-mapping The configuration section appears in order to call the Dofilter () method in turn.

And finally the servlet.

<servlet></servlet> is used to declare data for a servlet, mainly with the following child elements:

<servlet-name></servlet-name> Specify the name of the servlet

<servlet-class></servlet-class> specifying the class name of the servlet

<jsp-file></jsp-file> Specify the full path of a JSP Web page in the Web platform

<init-param></init-param> is used to define parameters and can have multiple init-param. Accessing initialization parameters in a servlet class through the Getinitparamenter (String name) method

<load-on-startup></load-on-startup> specifies the order in which the servlet is loaded when the Web App starts.

The <servlet-mapping></servlet-mapping> server typically provides a default url:http://host/webappprefix/servlet/servletname for Servlets.

However, this URL is often changed so that the servlet can access the initialization parameters or make it easier to handle relative URLs. Use the servlet-mapping element when changing the default URL.

Used to define a URL for a servlet that contains two child elements

<servlet-name></servlet-name> Specify the name of the servlet

<url-pattern></url-pattern> Specify the URL for the servlet

Where the <load-on-startup></load-on-startup> value is positive or zero: the servlet container loads the servlet with a small number, and then loads the other large servlet in turn. The value is negative or undefined: the servlet container loads it when the Web client first accesses the servlet.

Context-param,listener,filter,servlet loading sequence of Web. XML and its surrounding

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.