Web. xml is used in general web Projects. web. xml is mainly used for configuration and convenient web project development. Web. xml is mainly used to configure filters, Listener, and Servlet. However, it should be noted that web. xml is not necessary. a web project can have no web. xml file.
Detailed description of the web. xml file
Web. xml is used in general web Projects. web. xml is mainly used for configuration and convenient web project development. Web. xml is mainly used to configure filters, Listener, and Servlet. However, it should be noted that web. xml is not necessary. a web project can have no web. xml file.
1. WEB project load web. xml process
In my personal tests, the order of loading the WEB project is irrelevant to the configuration order of the element nodes in the file. That is, the filter will not be loaded first because the filter is written before the listener. The loading sequence of WEB containers is ServletContext-> context-param-> listener-> filter-> servlet. These elements can be configured anywhere in the file.
The loading process is as follows:
-
When starting a WEB project, the WEB container will read its configuration file web. xml, read And Two nodes.
-
Urgently, create a ServletContext, and all parts of the web project will share the context.
-
Container Convert to a key-value pair and submit it to servletContext.
-
Container creation To create a listener.
2. detailed description of web. xml file elements
1. schema
The pattern file of web. xml is defined by Sun, and the root element of each web. xml file Must indicate which pattern file is used for the web. xml file. All other elements are stored in .
2, Web application icon
It is pointed out that IDE and GUI tools are used to represent the big and small icons of Web applications.
/images/app_small.gif
/images/app_large.gif
3, Web application name
A gui tool may be used to mark the name of a specific Web application.
Tomcat Example
4, Web Application Description
This descriptive text is provided.
Tomcat Example servlets and JSP pages.
5, Context parameters
Declare the initialization parameters within the application range. It 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. You can use getServletContext (). getInitParameter ("context/param") in the servlet.
ContextParameter
test
It is a test parameter.
6, Filter
Associate a name with a class that implements the javaxs. servlet. Filter interface.
setCharacterEncoding
com.myTest.setCharacterEncodingFilter
encoding
UTF-8
setCharacterEncoding
/*
7, Listener
com.listener.SessionListener
8,
The data used to declare a servlet mainly includes the following sub-elements:
-
Servlet name
-
Specify the servlet class name
-
Specifies the complete path of a JSP page in the web platform
-
Used to define parameters. multiple init-param parameters are allowed. Access the initialization parameters through the getInitParamenter (String name) method in the servlet class.
-
Specify the order of Servlet loading when the Web application starts. When the value is positive or zero: the Servlet container first loads the servlet with a small value, and then loads the servlet with a large value in sequence. The value is negative or undefined: the Servlet container loads the servlet when the Web client first accesses the Servlet.
-
Defines the URL corresponding to the servlet. It contains two child elements.
-
Servlet name
-
Specify the URL corresponding to the servlet
Snoop
SnoopServlet
Snoop
/Snoop
Snoop
SnoopServlet
Foo
Bar
Security role for anonymous access
Tomcat
Snoop
/Snoop
9, Session Timeout configuration
The unit is minute.
120
10,
htm
text/html
11, Welcome file page
index.jsp
index.html
index.htm
12, Error Page
404
/NotFound. jsp
Java. lang. NullException
/Error. jsp
13, Set jsp
Including And Two child elements. Where Elements already exist in JSP 1.2; Is an element added to JSP 2.0.
The elements mainly have eight child elements:
-
: Set description
-
: Set name
-
: Set the impact range of the value, such as/CH2 or/*. jsp.
-
: If true, EL syntax is not supported.
-
: If true, the <% scripting %> syntax is not supported.
-
: Sets the encoding of JSP web pages.
-
: Set the JSP page header with the extension. jspf
-
: Set the end of the JSP webpage with the extension. jspf
Taglib
/WEB-INF/tlds/MyTaglib.tld
Special property group for JSP Configuration JSP example.
JSPConfiguration
/jsp/*
true
GB2312
true
/include/prelude.jspf
/include/coda.jspf
For Web applications, Scriptlet is an undesirable thing, because it will make HTML and Java code mixed, it is quite troublesome for program maintenance, when necessary, you can. add in xml Tag to prevent all JSP pages from using Scriptlet.
3. Mapping Rules
When a request is sent to the servlet container, the container first uses the requested url minus the current application context path as the servlet ing url. for example, I access http: // localhost/test/aaa.html. my application context is test. the container removes http: // localhost/test, and the rest of the/aaa.html part is used for servlet ing matching. This ing matching process is sequential, and when a servlet matches successfully, it will not ignore the remaining servlet.
The matching rules and sequence are as follows:
-
Exact path matching. Example: for example, if the url-pattern of servletA is/test and the url-pattern of servletB is/*, if the url I access is http: // localhost/test, in this case, the container will first perform exact path matching and find that/test is exactly matched by servletA. then, the container will call servletA and ignore other servlets.
-
Longest path match. Example: the url-pattern of servletA is/test/*, while the url-pattern of servletB is/test/a/*. when you access http: // localhost/test/, the container will select the servlet with the longest path to match, that is, the servletB here.
-
Extension match. if the last segment of the url contains extensions, the container selects the appropriate servlet based on the extension. Example: servletA url-pattern: *. action
What starts with "/" and ends with "/*" is used for path ING. The extension ING starts. Therefore, why does the "/*. action" such a seemingly normal matching look wrong? Because this match is a path ING and also an extension ING, the container cannot judge.
The above is a detailed analysis of the content of the web. xml file. For more information, see other related articles in the first PHP community!