Web. xml file detailed

Source: Internet
Author: User

Web. xml file detailed

General Web Engineering will use Web.xml,web.xml mainly used for configuration, can be convenient to develop Web engineering. Web. XML is primarily used to configure filter, Listener, servlet, etc. However, it is important to note that Web. XML does not have to be required, and it can be done without.

1. Web project load XML process

After personal testing, the Web project load order is independent of the order in which the element nodes are configured in the file. That is, the filter is not loaded before the filter is written in front of the listener. The Web container is loaded in the following order: ServletContext, Context-param, listener, filter, and servlet. And these elements can be configured anywhere in the file.

The loading process is in the following order:

    1. When you start a Web project, the Web container reads its config file, XML, and reads <listener> and <context-param> two nodes.
    2. Urgently, to create a ServletContext (servlet context), all parts of this Web project will share this context.
    3. The container converts <context-param> to a key-value pair and gives it to ServletContext.
    4. The container creates a class instance in <listener>, creating the listener.

2. Web. xml file element detailed

  1. Schema

The schema file for Web. XML is defined by sun, and the root element of each Web. xml file, <web-app>, must indicate which schema file this Web. XML uses. Other elements are placed in <web-app></web-app>.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"     xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"    xsi:schemalocation= "http://java.sun.com/xml/ns/ Java         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "></web-app>

  2. <icon>web application icon

Indicates the large icons and small icons that the IDE and GUI tools use to represent web apps.

<icon>    <small-icon>/images/app_small.gif</small-icon>    <large-icon>/images/app_ Large.gif</large-icon></icon>

  3. <display-name>web Application Name

A name that provides a GUI tool that may be used to mark this particular web App

<display-name>tomcat example</display-name>

  4. <disciption>web Application Description

Give descriptive text that is relevant for this

<disciption>tomcat Example Servlets and JSP pages.</disciption>

  5. <context-param> Context Parameters

Declares an application-scoped initialization parameter. It is used to provide ServletContext with key-value pairs, which are application context information. Our listener, filter, and so on, will use the information in these contexts when initializing. The servlet can be obtained by Getservletcontext (). Getinitparameter ("Context/param").

<context-param>    <param-name>ContextParameter</para-name>    <param-value>test< /param-value>    <description>it is a test parameter.</description></context-param>

  6. <filter> Filter

Associates a name with a class that implements the Javaxs.servlet.Filter interface.

<filter>    <filter-name>setCharacterEncoding</filter-name>    <filter-class> com.mytest.setcharacterencodingfilter</filter-class>    <init-param>        <param-name> encoding</param-name>        <param-value>UTF-8</param-value>    </init-param></ filter><filter-mapping>    <filter-name>setCharacterEncoding</filter-name>    < Url-pattern>/*</url-pattern></filter-mapping>

  7. <listener> Listener

<listener>     <listerner-class>com.listener.SessionListener</listener-class> </listener >

  8. <servlet>

<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> specifying the class name of the servlet
    • <jsp-file></jsp-file> Specify the full path of a JSP Web page in the Web platform
    • <init-param></init-param> is used to define parameters and can have multiple init-param. Accessing initialization parameters in a servlet class through the Getinitparamenter (String name) method
    • <load-on-startup></load-on-startup> specifies the order in which the servlet is loaded when the Web App starts. The value is 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 client first accesses the servlet.
    • <servlet-mapping></servlet-mapping> used to define the URL for the servlet, containing two child elements
    • <servlet-name></servlet-name> Specify the name of the servlet
    • <url-pattern></url-pattern> Specify the URL for the servlet
<!--Basic Configuration-<Servlet><Servlet-name>snoop</Servlet-name><Servlet-class>snoopservlet</Servlet-class></Servlet><Servlet-mapping><Servlet-name>snoop</Servlet-name><Url-pattern>/snoop</Url-pattern></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 Anonymous access</Description><Role-name>tomcat</role-name> </ run-as></ servlet>< servlet-mapping> < servlet-name>snoop</servlet-name> < Url-pattern>/snoop</url-pattern< Span data-mce-mark= "1" >></servlet-mapping >              

  9. <session-config> Session Timeout Configuration

Unit is minutes.

<session-config>    <session-timeout>120</session-timeout></session-config>

10. <mime-mapping>

<mime-mapping>    <extension>htm</extension>    <mime-type>text/html</mime-type ></mime-mapping>

11. <welcome-file-list> Welcome File page

<welcome-file-list>    <welcome-file>index.jsp</welcome-file>    <welcome-file> Index.html</welcome-file>    <welcome-file>index.htm</welcome-file></welcome-file-list >

12. <error-page> error page

<!--1, through the error code to configure the Error-page. When a XXX error occurs on the system, jump to the error handling page.  -<Error-page>    <Error-code>404</Error-code>    < Location>/notfound.jsp</ Location></Error-page><!--2. Configure Error-page by the type of exception. Jumps to the error handling page when the system occurs java.lang.NullException (that is, null pointer exceptions).  -<Error-page>    <Exception-type>Java.lang.NullException</Exception-type>    < Location>/error.jsp</ Location></Error-page>

  13. <jsp-config> set up JSP

<jsp-config> includes <taglib> and <jsp-property-group> two child elements. Where the <taglib> element already exists at JSP 1.2, and <jsp-property-group> is the new element of JSP 2.0.

The <jsp-property-group> element consists of eight sub-elements, each of which are:

    • <description>: set description  
    • <display-name>: set name  
    • <url-pattern> : The range affected by the setpoint, such as: /ch2  or  /*.jsp
    • <el-ignored>: If it is  true, it does not support  EL  grammar  
    • <scripting-invalid>: If  true,  <% scripting%> syntax   is not supported;
    • < PAGE-ENCODING>: Set  JSP  page encoding  
    • <include-prelude>: Settings  JSP  page header, extension &NBSP;.JSPF
    • <include-coda>: Settings  JSP  end of Web page with extension &NBSP;.JSPF
 <jsp-config> <taglib> <taglib-uri>Taglib</taglib-uri> <taglib-location> ;/web-inf/tlds/mytaglib.tld</taglib-location> </taglib> <jsp-property-group> <descriptio N>special Property Group for JSP Configuration JSP example.</description> <display-name>jspconfigura tion</display-name> <url-pattern>/jsp/* </url-pattern> <el-ignored>true</el-ignor Ed> <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> 

For Web applications, Scriptlet is an unwanted sight, because it makes the HTML and Java code Cross-mixed, for the maintenance of the program is quite troublesome, when necessary, you can add <script-invalid in Web. xml > tags, set all JSP pages can not use Scriptlet.

3. Mapping rules

When a request is sent to the servlet container, the container first subtracts the requested URL from the path of the current application context as a mapped URL for the servlet, for example, I am visiting http://localhost/test/ aaa.html, my application context is test, the container will remove Http://localhost/test, and the rest of the/aaa.html to do the servlet mapping match. The mapping matching process is sequential, and when a servlet match succeeds, the remaining servlet is not ignored.

The matching rules and Order are as follows:

    1. Exact path matching. Example: For example, Servleta Url-pattern for/TEST,SERVLETB url-pattern for * *, this time, if I access the URL is http://localhost/test, this time the container will be the exact path matching , found that the/test is precisely matched by Servleta, then go to call Servleta, will not ignore the other servlet.
    2. Longest path match. Example: Servleta's Url-pattern is/test/*, and Servletb's Url-pattern is/test/a/*, at this time when the http://localhost/test/a is visited, The container chooses the longest path of the servlet to match, which is where the SERVLETB.
    3. Extension matches, if the last paragraph of the URL contains an extension, the container will select the appropriate servlet based on the extension. Example: Servleta's Url-pattern:*.action

Starting with "/" and ending with "/*" are used to do the path mapping. Previous prefix "*." The beginning is used to do the extension mapping. So why is it wrong to define "/*.action" as a seemingly normal match? Because this match belongs to the path mapping, also belongs to the extension mapping, causes the container to be unable to judge.

Web. xml file detailed

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.