Loading sequence of listener, filter and servlet in Web.xml and its detailed explanation
I. Overview
1. When starting a Web project, the Web container reads its profile web.xml, reads <listener> and <context-param> two nodes.
2. Urgently, create a ServletContext (servlet context), all parts of this Web project will share this context.
3. The container will convert <context-param> to the key value pair and give it to ServletContext.
4, the container creates the class instance in the <listener>, creates the listener.
Second, Load-on-startup
The Load-on-startup element specifies the order in which the servlet is loaded when the Web application is started, and its value must be an integer. If its value is a negative integer or the element does not exist, the container loads the servlet when the servlet is invoked. If the value is a positive integer or 0, the container loads and initializes the servlet when it is configured, and the container must ensure that the small value is loaded first. If the values are equal, the container can automatically choose who to load first.
The meaning of,<load-on-startup>5</load-on-startup> in the servlet configuration is:
Whether the tag 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 application is started;
When a negative number is or is not specified, the container is indicated to be loaded when the servlet is selected.
The smaller the value of a positive number, the higher the priority of starting the servlet.
Third, loading sequence
The first thing to be sure is that the load order is independent of the order in which they are in the Web.xml file. The filter is not loaded first because 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 a key-value pair to ServletContext, the application context information. Our listener, filter, and so on are initialized with the information in these contexts, so the Context-param configuration section should be written in front of the Listener configuration section. In fact, the Context-param configuration section can be written anywhere, so the true loading order is: Context-param-> listener-> filter-> servlet
For a class of configuration sections, they are related to the order in which they appear. Take filter For example, Web.xml can define multiple filter, of course, a configuration section related to filter is filter-mapping, it must be noted here, for the same filter-name filter and Filter-mappin In the case of the G configuration section, the filter-mapping must appear after the filter, otherwise, when parsing to filter-mapping, its corresponding filter-name is undefined. When each filter is initialized at the time the Web container is started, it is initialized in the order in which the Filter configuration section appears, and when the requested resource matches multiple filter-mapping, the filter interception resource is called in turn according to the order in which the Filter-mapping configuration section appears Ofilter () method.
The servlet is similar to filter, and is no longer being repeat here.
From this, we can see that the Web.xml loading order is: ServletContext-> context-param-> Listener filter-> servlet, The order of the actual program calls between the same types is invoked according to the order of the corresponding mapping.
All the other elements are in <web-app></web-app>.
<discription></discription> is a description of the platform
<display-name></display-name> define the name of the platform
<distributable/> Specifies whether the platform can be distributed for processing
<context-param></context-param> to set the environment parameters for the Web site, which contains two child elements:
<param-name></param-name> to specify the name of the parameter
<param-value></param-value> to set parameter values
Like what:
<context-param>
<param-name>my_param</param-name>
<param-value>hello</param-value>
</context-param>
The parameters that are set here can be obtained using the Getservletcontext (). Getinitparameter ("My_param") in the servlet.
2, Listener
<listener></listener> to set the listener interface, its primary child element is
<listener-class></listener-class> define the class name of the listener
Like what:
<listener>
<listener-class>com.myTest.ContextListener</listener-class>
</listener>
3.1, Filter
<filter></filter> is used to declare the relevant settings for the filter, which contains the following child elements:
<filter-name></filter-name> This is, of course, the name of the specified filter
<filter-class></filter-class> This is the name of the class used to define Filter
<init-param></init-param> to define parameters, it has two child elements:
<param-name></param-name> to specify the name of the parameter
<param-value></param-value> to set parameter values
Like what:
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
3.2, filter-mapping
At the same time, <filter></filter> uses the URL that <filter-mapping></filter-mapping> uses to define the filter, which has two child elements:
<filter-name></filter-name> Specify the name of the filter
<url-pattern></url-pattern> Specify the URL of the filter
Like what:
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.1. servlet
<servlet></servlet> is used to declare data for a servlet, with the following child elements:
<servlet-name></servlet-name> Specify the name of the servlet
<servlet-class></servlet-class> specify the class name of the servlet
<jsp-file></jsp-file> Specify the full path of a JSP page in the Web site
<init-param></init-param> used to define parameters, similar to the previous <init-param>
4.2, Servlet-mapping
Used with <servlet></servlet> is <servlet-mapping></servlet-mapping> used to define the URL for the servlet. Contains two child elements:
<servlet-name></servlet-name> Specify the name of the servlet
<url-pattern></url-pattern> Specify the URL for the servlet
Like what:
<servlet>
<servlet-name>ShoppingServlet</servlet-name>
<servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShoppingServlet</servlet-name>
<url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>
5, Session-config
<session-config></session-config> to define the session parameters in the Web site, including a child element:
<session-timeout></session-timeout> to define the expiration date, in minutes, for all sessions of this web platform
6, Mime-mapping
<mime-mapping></mime-mapping> defines an extension and a MIME type to be mapped, containing two child elements:
<extension></extension> name of the extension
<mime-type></mime-type> MIME Format
Like what:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
7, Welcom-file-list
<welcome-file-list></welcom-file-list> to define a list of the first page, containing a child element:
<welcome-file></welcome-file> Specify the file name of the first page
Like what:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcom-file-list>
8, Error-page
<error-page></error-page> a page that handles error codes or exceptions with three child elements:
<error-code></error-code> Specify error codes
<exception-type></exception-type> Specify a Java exception type
<location></location> specify the relevant resource path within the Web site
Like what:
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception.jsp</location>
</error-page>
9, Taglib
<taglib></taglib> the tag library path used to set up the JSP Web page, with two child elements:
<taglib-uri></taglib-uri> define the URI of the TLD file, and use the taglib instruction in the JSP Web page to get the TLD file of the URI
<taglib-location></taglib-location> specify where the TLD file is stored relative to the Web platform
Like what:
<taglib>
<taglib-uri>myTaglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
10, Resource-ref
<resource-ref></resource-ref> defines a five-child element that utilizes Jndi to obtain the resources available to the platform:
<description></description> Resource Description
<rec-ref-name></rec-ref-name> Resource Name
Types of <res-type></res-type> Resources
<res-auth></res-auth> resources licensed through application or container
<res-sharing-scope></res-sharing-scope> whether resources can be shared, there are shareable and unshareable two Value, default is shareable
For example, configuring a database connection pool can be configured here:
<resource-ref>
<description>jndi JDBC DataSource of shop</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
11, Jsp-config
<jsp-config> includes <taglib> and <jsp-property-group> two child elements.
Where <taglib> elements already exist in JSP 1.2, <jsp-property-group> is the new element of JSP 2.0.
The <jsp-property-group> elements are mainly eight child elements, respectively:
1). <description>: Description of the setting;
2). <display-name>: set name;
3). <URL-PATTERN>: The range affected by the set value, such as:/ch2 or/*.jsp;
4). <el-ignored>: If true, indicates that El syntax is not supported;
5). <scripting-invalid>: If true, indicates that <% scripting%> syntax is not supported;
6). <page-encoding>: Set the code of JSP Web page;
7). <include-prelude>: Sets the head of the JSP Web page, with the extension. JSPF;
8). <include-coda>: Sets the end of the JSP Web page with the extension. JSPF.
A simple <jsp-config> element complete configuration:
<jsp-config>
<taglib>
<taglib-uri>Taglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<jsp-property-group>
<description>special Property Group for JSP Configuration JSP example.</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>/jsp/* </url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GB2312</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/include/prelude.jspf</include-prelude>
<include-coda>/include/coda.jspf</include-coda>
</jsp-property-group>
</jsp-config>
Configure Web.xml to restrict requests to certain servlet
Sometimes we only want the authenticated user to request some servlet, and we can do it in the web.xml for this purpose.
This will use the <security-constraint></security-constraint> element.
For Tomcat, web.xml using security-constraint elements is required in the <tomcat-installation-directory>/conf/ Create a username and password in the Tomcat-users.xml XML file. For example, the following Tomcat-users.xml file:
This XML fragment includes a tomcat-users root element that contains one or more role and user elements.
The Security-constraint, Login-config, and security-role elements are then created in the Web.xml of the Web application.
The Security-constraint element contains one or more web-resource-collection elements that describe which Web resources in the Web application are protected by the specified security restrictions. The Http-method element specifies the HTTP method that the security limit overrides. In the example above, the configured security is triggered when we apply a/helloservlet get or POST request.
The Auth-constraint element is used to describe security roles that allow access to Web components. Examples of security roles in this example are Tomcat, manager, admin. Only users who are in the admin role can access helloservlet.
The Web application authenticates the user by Login-config elements and confirms that the user is the correct role.
Longin-config contains transport-guarantee child elements used to specify authentication methods, basic is a common Web authentication method, the browser to the user prompts a dialog box, asking for a username and password, Tomcat then compares the username and password given to the username and password in tomcat-users.xml, and then uses the previous Security-constraint configuration to determine whether the user has access to the protected servlet.
(In addition to basic, can also be form, Client-cert, digest, etc.)
In fact, this authentication method actually has two steps:
1, check the supplied username and password is correct.
2, to determine whether the user mapped to a specific security role. For example, a user may provide the correct user name and password, but not mapped to a specific security role, and will be prevented from accessing a particular Web resource.
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