In the project will always encounter some of the priority issues of loading, the recent also encountered similar, so their own to find data summarized below, some of the following are reproduced others, after all, people write well, they do not repeat the wheel, just slightly point their own modification.
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: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 when the Web container is started, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, thefilter interception resource is based on the filter-mapping The order in which the configuration section appears is called in turn to the Dofilter () method.
The servlet is similar to filter , and is no longer being repeat here.
Thus, it can be seen that the loading order of the Web.xml is:context-param-> Listener-> filter-> servlet , and the order of the actual program calls between the same types is based on the corresponding Called in the order of the mapping.
Web.xml Document Detailed
Web.xml Common Elements <web-app> <display-name></display-name> defines the name of the Web application <description></
Description> declares the description of the Web application <context-param></context-param> context-param element declares the initialization parameters in the application scope.
The <filter></filter> filter element associates a name with a class that implements the Javax.servlet.Filter interface.
<filter-mapping></filter-mapping> Once you have named a filter, use the filter-mapping element to associate it with one or more servlet or JSP pages.
Version 2.3 of the <listener></listener>servlet API adds support for event listeners, which are notified when the session or servlet environment is created, modified, and deleted by the event listener.
The listener element indicates an event listener class. <servlet></servlet> when you make initialization parameters or custom URLs to a servlet or JSP page, you must first name the servlet or JSP page.
The servlet element is used to accomplish this task. <servlet-mapping></servlet-mapping> servers typically provide a default url:http://host/webappprefix/servlet/for the servlet
Servletname. However, this URL is often changed so that the servlet can access the initialization parameters or more easily handle relative URLs.
The servlet-mapping element is used when changing the default URL.
<session-config></session-config> If a session is not accessed for a certain amount of time, the server can discard it to save memory. You can explicitly set a time-out value for a single Session object by using the HttpSession Setmaxinactiveinterval method, or you canSet the default timeout value with the Session-config element.
<mime-mapping></mime-mapping> If Web applications have the idea of special files and want to guarantee that they are assigned a specific MIME type, the mime-mapping element provides this assurance.
<welcome-file-list></welcome-file-list> instructs the server to use which file when it receives a URL that references a directory name instead of a filename.
<error-page></error-page> when a particular HTTP status code is returned, or when a particular type of exception is thrown, it is possible to make the page that will be displayed. <taglib></taglib> specifies an alias for the tag Library descriptor file (tag Libraryu descriptor file).
This feature allows you to change the location of the TLD file without editing the JSP page that uses the files.
<resource-env-ref></resource-env-ref> declare a resource-related management object.
<resource-ref></resource-ref> declare an external resource used by a resource factory. <security-constraint></security-constraint> develop URLs that should be protected. It is used in conjunction with the Login-config element <login-config></login-config> specifies how the server should authorize users attempting to access a protected page.
It is used in conjunction with the Sercurity-constraint element. <security-role></security-role> gives a list of security roles that will appear in the Security-role-ref element within the servlet element Role-na Me child element.
Declaring roles separately makes it easier for the advanced IDE to handle security information.
<env-entry></env-entry> declare environment items for Web applications.
<ejb-ref></ejb-ref> declares a reference to the home directory of an EJB. ,;
ejb-local-ref></Ejb-local-ref> declares the application of an EJB's local home directory. </web-app> corresponding element configuration 1, Web application icon: Indicates large icons and small icons used by IDE and GUI tools to represent Web applications <icon> <small-icon>/images/app_small.gi F</small-icon> <large-icon>/images/app_large.gif</large-icon> </icon> 2, Web
Application Name: Provides a name that the GUI tool might use to mark this particular Web application <display-name>tomcat example</display-name> 3, Web Application Description: Give descriptive text related to this
<disciption>tomcat Example servlets and JSP pages.</disciption> 4, context parameters: declaring the initialization parameters within the scope of the application. <context-param> <param-name>ContextParameter</para-name> <param-value>test</param-val Ue> <description>it is a test parameter.</description> </context-param> within the servlet can be passed gets
Ervletcontext (). Getinitparameter ("Context/param") gets 5, filter configuration: Associates a name with a class that implements the Javaxs.servlet.Filter interface. <filter> <filter-name>setCharacterEncoding</filter-name> <filter-class>com.mytes T.setcharacterencodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value> </init-param> </filter> <filter-mapping&
Gt <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </fil Ter-mapping> 6, listener configuration <listener> <listerner-class>listener. Sessionlistener</listener-class> </listener> 7, servlet configuration basic configuration <servlet> <servlet-n
Ame>snoop</servlet-name> <servlet-class>SnoopServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>snoop</servlet-name> <url-pattern>/snoop</url-pat
Tern> </servlet-mapping> Advanced Configuration <servlet> <servlet-name>snoop</servlet-name> <servlet-class>snoopservlet</servlet-class> <init-param> <param-name>foo</param-name> <param-value>bar </param-value> </init-param> <run-as> <description>security role for Anony MOUs access</description> <role-name>tomcat</role-name> </run-as> </servle t> <servlet-mapping> <servlet-name>snoop</servlet-name> <URL-PATTERN>/SNOOP&L
The t;/url-pattern> </servlet-mapping> element description <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> the class of the specified servlet Name <jsp-file></jsp-file> Specify the full path <init-param></init-param> of a JSP page in the Web site to define parameters, and you can have multiple in It-param. To access the initialization parameters in the Servlet class through the Getinitparamenter (String name) method <load-on-startup></load-on-startup> specify that when a Web application starts,
The order in which the servlet is loaded. The value is either 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 customer first accesses the servlet <servlet-mapping></servlet-mapping> defines the URL for the servlet. Contains two child elements <servlet-name></servlet-name> specifies the name of the servlet <url-pattern></url-pattern> specified s Ervlet URL 8, Session timeout configuration (in minutes) <session-config> <session-timeout>120</session-timeout> &L T;/session-config> 9, MIME type configuration <mime-mapping> <extension>htm</extension> <mime-t Ype>text/html</mime-type> </mime-mapping> 10, specify Welcome File page configuration <welcome-file-list> <welcom E-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-fil E>index.htm</welcome-file> </welcome-file-list> 11, configure error page One, configure Error-page by error code <error-page&
Gt <error-code>404</Error-code> <location>/NotFound.jsp</location> </error-page> The above is configured to jump to the error-handling page when a 404 error occurs on the system
Notfound.jsp.
Ii. Configuring Error-page <error-page> <exception-type>java.lang.NullException</exception-type> by type of exception <location>/error.jsp</location> </error-page> The above configuration is configured when the system occurs java.lang.NullException (that is, null pointer exception), Jump to Error-handling page error.jsp 12, TLD configuration <taglib> <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib<
;/taglib-uri> <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location> </taglib> If the MyEclipse has been the error, should put <taglib> into <jsp-config> <jsp-config> <taglib> < Taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> <taglib-location>/WEB-INF/ Pager-taglib.tld</taglib-location> </taglib> </jsp-config> 13, Resource management Object configuration <resource-env-
Ref> <resource-env-ref-name>jms/StockQueue</resource-env-ref-name> </resource-env-ref> 14, Resource factory configuration <resource-ref> <res-ref-name>mail/Session</res-ref-name> <res-type>javax.mail.
The session</res-type> <res-auth>Container</res-auth> </resource-ref> configuration database connection pool can be configured here: <resource-ref> <description>jndi JDBC DataSource of shop</description> <res-ref-n Ame>jdbc/sample_db</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-a Uth>container</res-auth> </resource-ref> 15, security restriction configuration <security-constraint> <display- Name>example Security constraint</display-name> <web-resource-collection> <web-resource -name>protected area</web-resource-name> <URL-PATTERN>/JSP/SECURITY/PROTECTED/*</URL-PATTERN&G
T