1. When starting a Web project, the Web container reads its config file, XML, and reads <listener> and <context-param> two nodes.
2, emergency, allow to create a ServletContext (servlet context), all parts of this Web project will share this context.
3, the container will be <context-param> converted to a key value pair, and handed to ServletContext.
4. Create a listener for the class instance in the <listener> container.
Two Load-on-startup
The Load-on-startup element specifies the order in which the servlet is loaded when the Web application is launched, and its value must be an integer. If its value is a negative integer or the element does not exist, the container will load the servlet when the servlet is called. If the value is a positive integer or 0, the container loads and initializes the servlet when configured, and the container must ensure that the value is loaded first. If the values are equal, the container can automatically select who to load first.
In the configuration of the servlet, the meaning of,<load-on-startup>5</load-on-startup> is:
Flags whether the 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 app is started;
When it is a negative number or is not specified, it indicates that the container is loaded only when the servlet is selected.
The lower the value of a positive number, the higher the priority to start the servlet.
Three-load order
The first thing to be sure is that the order of loading is independent of their order in the Web. xml file. That is, the filter is not loaded before 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 ServletContext with key-value pairs, which are application context information. Our listener, filter, and so on are initialized with the information in these contexts, so should the Context-param configuration section be written before the Listener configuration section? In fact, the Context-param configuration section can be written anywhere, so the real load order is: Context-param, listener, filter, servlet
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 at the start of the Web container, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, the filter interception resource is invoked in the order in which the Filter-mapping configuration section appears in sequence D The Ofilter () method.
The servlet is similar to filter and is not mentioned here.
From this, it can be seen that the loading order of Web. XML is: ServletContext, Context-param-A, listener-a servlet, The order of the actual program calls between the same types is called according to the Order of the corresponding mapping.
1. Web. XML is called the deployment descriptor file, which is defined in the servlet specification and is the configuration file for the application.
2. The deployment descriptor file, like all XML files, must start with an XML header. This header declares the XML version that can be used and gives the character encoding of the file. The DOCYTPE declaration must immediately appear after this header. This statement tells the server which version of the servlet specification is applicable (for example, 2.2 or 2.3) and specifies a DTD (document type definition, doc-types definitions) that manages the syntax for the remainder of this file. The top-level (root) element of all deployment descriptor files is Web-app. Note that XML elements are not like HTML, and they are case-sensitive. Therefore, Web-app and Web-app are not legal, web-app must be lowercase.
Web. XML Sample:
- <? Xml version= "1.0" Encoding= "UTF-8" ?>
- <! doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en " " HTTP://JAVA.SUN.COM/DTD/WEB-APP_2_3.DTD ">
- <web-app>&NBSP;&NBSP;
- </web-app>
3. XML elements are not only case-sensitive, but they are also sensitive to the order in which they appear in other elements. For example, the XML header must be the first item in the file, the DOCTYPE declaration must be the second item, and the Web-app element must be the third item. Within the Web-app element, the order of the elements is also important. Servers do not necessarily enforce this order, but they allow (in fact some servers do) a complete denial of the execution of Web applications that contain elements that are not in the correct order. This means that the Web. xml file using the non-standard element order is not portable.
Element Order list:
The element tag is detailed:
Element 1:<icon>
Meaning
The icon element contains the Small-icon and Large-icon two child elements. Used to specify the path to small icons and large icons on the Web platform.
<small-icon>/Path/smallicon.gif</small-icon>
The Small-icon element should point to the path of a small icon in the Web site, with a size of X pixel, but the image file must be in GIF or JPEG format, with the extension must be:. gif or. jpg.
<large-icon>/Path/largeicon-jpg</large-icon>
The Large-icon element should point to a large chart path in the Web site, with a size of X-pixel, but the image file must be in GIF or JPEG format and the extension must be; GIF or JPG.
Example
<icon>
<small-icon>/images/small.gif</small-icon>
<large-icon>/images/large.gif</large-icon>
</icon>
Element 2, 3:<display-name>,<description>
Meaning
<display-name> App Name </display-name>
Defines the name of the app.
<description> Application Description </discription>
Make a description of the application.
Example
<display-name>test</display-name>
<description> Test Application V1.0</discription>
Element 4:<context-param>
Meaning
The Context-param element is used to set the environment parameter (context) for a Web application that contains two child elements:
Param-name and Param-value.
<param-name> Parameter name </param-name>
Set the context name
<param-value> Values </param-value>
Set the value of the context name
Example
<context-param>
<param-name>param_name</param-name>
<param-value>param_value</param-value>
</context-param>
The parameters that are set in the JSP Web page can be obtained using the following methods:
${initparam.param_name}
If the servlet can use the following methods to obtain:
String Param_name=getservletcontext (). Getinitparamter ("Param_name");
Element 5,6:<filter>,<filter-mapping>
Meaning
The filter element is used to set the filters for the Web application, and its two main sub-elements filter-name and filter-class are used to define the class of the filter
Name of <filter-name>filter </filter-name>
Define the name of the filter
Class name of <filter-class>filter </filter-class>
Define the class name of the filter
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.
Name of <filter-name>filter </filter-name>
Defines the name of the filter.
<url-pattern>URL</url-pattern>
The rul corresponding to Filter. For example:<url-pattern>/filter/*</url-pattern>
Example
<filter>
<filter-name>Encoding</filter-name>
<filter-class>ghjf.test.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Element 7:<listener>
Meaning
The listener element is used to define the listener interface, and its primary child element is <listener-class>
Class name of <listen-class>listener </listener-class>
Defines the class name of the listener
<listener>
Example
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Element 8, 9:<servlet>, <servlet-mapping>
Meaning
The two main child elements of the servlet element, Servlet-name and Servlet-class, define the class that the servlet corresponds to
Name of <servlet-name>servlet </servlet-name>
Define the name of the servlet
Class name of <servlet-class>servlet </servlet-class>
Define the class name of the servlet
The servlet-mapping element contains two child elements servlet-name and url-pattern. Used to define the URL of the servlet.
Name of <servlet-name>servlet </servlet-name>
Defines the name of the servlet.
<url-pattern>servlet url</url-pattern>
Defines the rul that the Servlet corresponds to. For example:<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
Example
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>dwr servlet</display-name>
<description>direct Web remoter servlet</description>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
Element 10:<session-cofing>
Meaning
Session-config contains a child element, Session-timeout. Defines the session parameter in the Web App.
<session-timeout> minutes </session-timeout>
Defines the expiration date for all sessions of this web site. Units are minutes.
Example
<session-config>
<session-timeout>30</session-timeout>
</session-config>
Element 11:<mime-mapping>
Meaning
The mime-mapping contains two child elements extension and Mime-type. Defines an extension and a MIME type to be mapped.
<extension> extension name </extension>
Extension name
<mime-type>mime format </mime-type>
MIME format.
Example
<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>
<mime-mapping>
<extension>ppt</extesnion>
<mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>
Element 12:<welcome-file-list>
Meaning
The welcome-file-list contains a child element, welcome-file. Used to define the first page of the list.
<welcome-file> used to specify the home page file name </welcome-flie>
Welcome-file is used to specify the home page file name. We can use <welcome-file> to specify a few home pages, and the server will be in accordance with the set order to find the homepage.
Example
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
Element 13:<error-page>
Meaning
The Error-page element contains three child elements, Error-code,exception-type, and location. The type of error code or exception (exception) corresponds to the Web App resource path.
<error-code> Error Codes </error-code>
HTTP Error Code, for example: 404, 403
<exception-type>Exception</exception-type>
A full-name Java exception type
<location>/Path </location>
Related resource paths within a web app
Example
<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>
Element 14:<jsp-config>
Meaning
The Jsp-config element is primarily used to set the relevant configuration of the JSP,<jsp:config> including <taglib> and <jsp-property-group> two sub-elements. Where <taglib> Elements
It already exists at JSP 1.2, and <jsp-property-group> is the new element in JSP 2.0.
<taglib>
The Taglib element contains two child elements Taglib-uri and taglib-location. Used to set the tag library path used by the JSP Web page.
<taglib-uri>URI</taglib-uri>
Taglib-uri the taglib directive of the URI,JSP Web page that defines the TLD file can be accessed through this URI to the TLD file.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
The TLD file corresponds to the location where the Web site is stored.
</taglib>
<jsp-property-group>
The Jsp-property-group element contains 8 elements, respectively:
<description>Description</descrition>
Description of this setting
<display-name>Name</display-name>
The name of this setting
<url-pattern>URL</url-pattern>
The range affected by the setpoint, such as:/ch2 or/*.jsp
<el-ignored>true|false</el-ignored>
True to indicate that El syntax is not supported.
<scripting-invalid>true|false</scripting-invalid>
True indicates that the <%scription%> syntax is not supported.
<page-encoding>encoding</page-encoding>
Set the encoding of the JSP Web page
<include-prelude>.jspf</include-prelude>
Sets the header of the JSP Web page with the extension. JSPF
<include-coda>.jspf</include-coda>
Sets the end of the JSP Web page with the extension. JSPF
</jsp-property-group>
</jsp-config>
Example
<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>
<uri-pattern>/*</uri-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GB2312</page-encoding>
<scripting-inivalid>true</scripting-inivalid>
</jsp-property-group>
</jsp-config>
Element 15:<resource-env-ref>
Meaning
Resource-env-ref has two child elements:
<resource-env-ref-name> Resource Name </resource-env-ref-name>
The name of the resource is relative to Java:comp/env
<resource-env-ref-type> Resource class name returned when locating a resource </resource-env-ref-type>
The name of the Java class that is returned when the Web app looks for the resource
Example
<resource-env-ref>
<resource-env-ref-name>jdbc/mssql</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
Element 16:<resource-ref>
Meaning
The Resource-ref element consists of five child elements description,res-ref-name,res-type,res-auth,res-sharing-scope. Use Jndi to get the application available resources.
<description> Description </description>
Resource Description
<rec-ref-name> Resource Name </rec-ref-name>
Resource Name
<res-type> Resources Category </res-type>
Types of resources
<res-auth>application| Container</res-auth>
Resources are licensed by application or container
<res-sharing-scope>shareable| Unshareable</res-sharing-scope>
Whether the resource can be shared. Default value is shareable
Example
<resource-ref>
<description>jndi JDBC datasource</description>
<res-ref-name>jdbc/data</res-ref-name>
<res-type>javax.sql.DataSoruce</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Web. XML configuration Detailed