Java Web project listener filter servlet startup sequence and Configuration

Source: Internet
Author: User

I,
1. When starting a web project, the Web Container will read its configuration file web. xml and read <listener>
And <context-param>.
2. Urgent: Create a servletcontext. All parts of the web project will share the context.
3. The Container converts <context-param> to a key-Value Pair and sends it
Servletcontext. --- Some constants of the container
4. Create a class instance in the <listener> container and a listener.
-- Completed before the application is started
II,

The load-on-startup element specifies the order in which the servlet is loaded when the web application starts. Its value must be an integer. If its value isIf the negative integer or the element is not stored, the container loads the servlet when the servlet is called.. If the value isA positive integer or zero. The container loads and initializes the servlet during configuration,The volume must be loaded first when the value is small. If the values are equal, the container can automatically select who to load first.

--> It can be seen that 0 is first loaded, and then 1, 2, 3, and negative

In servlet configuration, <load-on-startup> 5 </load-on-startup> indicates:

Mark whether the servlet is loaded when the container is started.
If the value is 0 or greater than 0, the servlet is loaded when the application is started;

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

The smaller the positive value, the higher the priority of starting the servlet.

III,
There will always be some loading priority problems in the project, and similar problems have been encountered recently, so I searched for the information and summarized some of the following, after all, people write well, so they will not duplicate the wheel, but just add some modifications.
First of all, it is certain that the order of loading is irrelevant to their order in the web. xml file. That is, the filter will not be first loaded because the filter is written before the listener. The final conclusion is: listener-> filter-> Servlet
There is also a configuration section: Context-Param, which is used to provide key-value pairs to servletcontext, that is, application context information. Our listener and filter will use the information in these contexts during initialization. Should the context-Param configuration section be written before the Listener Configuration section? In fact, the context-Param configuration section can be written in any location, so the actual loading order is: Context-
Param-> listener-> filter-> Servlet
For certain configuration sections, the order in which they appear is related. Take filter as an example. of course, multiple filters can be defined in XML. One configuration section related to the filter is filter-mapping. Note that the filters and filter-mapping configurations with the same filter-name must be configured.
In this section, the filter-mapping must appear after the filter. Otherwise, the filter-name corresponding to the filter-mapping is not defined when the filter-mapping is parsed. When a Web container is started, each filter is initialized according to the sequence in the filter configuration section. When the requested resource matches multiple Filters-mapping, filter intercepts resources by calling the dofilter () method in sequence according to the order in the filter-mapping configuration section.
Servlet is similar to filter. We will not repeat it here.
From this, we can see that the web. the XML loading sequence is: Context-param-> listener-> filter-> servlet, the actual Program Calling sequence between the same types is called according to the corresponding Mapping Sequence.

Detailed description of the web. xml file
Common web. XML elements
<Web-app>
<Display-Name> </display-Name> defines the web application name.
<Description> </description> description of the Web Application

<Context-param> </context-param> the context-Param element declares the initialization parameters in the application scope.
<Filter> </filter> the filter element associates a name with a class that implements the javax. servlet. Filter interface.
<Filter-mapping> </filter-mapping> once a filter is named, the filter-mapping element is used to associate it with one or more servlet or JSP pages.
<Listener> </listener> servlet API Version 2.3 adds support for event listening programs. The event listening program is notified when a session is created, modified, or deleted.
The listener element specifies the event listener class.
<Servlet> </servlet> when initializing parameters or custom URLs to the servlet or JSP page, you must first name the servlet or JSP page. The servlet element is used to complete this task.
<Servlet-mapping> </servlet-mapping> the server generally provides the servlet with a default URL: http: // host/webappprefix/servlet/servletname.
However, this URL is often changed so that the servlet can access initialization parameters or process relative URLs more easily. When you change the default URL, use the servlet-mapping element.

<Session-config> </session-config> If a session is not accessed for a certain period of time, the server can discard it to save memory.
You can use the setmaxinactiveinterval method of httpsession to explicitly set the timeout value for a single session object, or use the session-config element to specify the default timeout value.

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.