On the configuration order of servlet, Filter, and listener in Web.xml

Source: Internet
Author: User
Tags auth define session tld tomcat server

(i) web.xml the loading sequence of different elements

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 loading order of the Web.xml is: ServletContext-> context-param-> Listener the filter-> servlet, and the order of the actual program calls between the same types is based on the corresponding m Apping in the order in which they are called.


(ii) Web.xml documents detailed

(2.1)

First, schema.
[HTML] view plain copy <web-app xmlns= "Http://java.sun.com/xml/ns/j2ee xmlns:xsi=" http://www.w3.org/200      1/xmlschema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "version=" 2.4 "> </web-app> Other elements are placed in <web-app></web-app>

<discription></discription> is a description of the site

<display-name></display-name> Define site Name

<distributable></distributable> Specifies whether the platform can be distributed for processing

(2.2)

Set the environment parameters for the Web site as follows [HTML] view plain copy <context-param> <param-name>my_param</param-name> <param-value>hello</param-value> </context-param>


There are also the following ways to set environment variables [HTML] view plain copy <env-entry> <env-entry-name>minexemptions</env-ent Ry-name> <env-entry-value>1</env-entry-value> <env-entry-type>java.lang.integer </env-entry-type> </env-entry>

After you set this parameter My_param, you can use Getservletcontext (). Getinitparameter ("My_param") in the servlet to obtain

(2.3) Filter Filters

Filter is set as follows: Elements <filter-name>, <filter-class>, <init-param>[HTML]View plain copy <filter> <filter-name>setCharacterEncoding</filter-name> <filter- Class>com.mytest.setcharacterencodingfilter</filter-class> <init-param> <param-n Ame>encoding</param-name> <param-value>GB2312</param-value> &LT;/INIT-PARAM&G   T </filter>

<filter-mapping> to and <filter> matching use settings are shown below <filter>[HTML]View Plain Copy <filter-mapping> <filter-name>setCharacterEncoding</filter-name> ;url-pattern>/*</url-pattern> </filter-mapping>
(2.4) Listener listener

Set the Set listener interface (mainly with child elements <listener-class></listener-class> define listener class names as follows)
[HTML] view plain copy <listener> <listener-class>com.mytest.contextlistener</listener-clas S> </listener>
(2.5) Servlet

The following methods define the servlet and servlet-mapping [HTML] view plain copy <servlet> <servlet-name>shoppingservle t</servlet-name> <!--Specify the servlet name--> <SERVLET-CLASS>COM.MYTEST.SHOPPINGSERVLET</SERVLET-CL Ass> <!--Specifies the servlet's class name (Implementation)--> </servlet> <servlet-mapping> <servlet-name>s Hoppingservlet</servlet-name> <url-pattern>/shop/ShoppingServlet</url-pattern> <!--specify Servle T corresponds to the url--> </servlet-mapping>

(2.6) Define session parameters

<session-config></session-config> to define the session parameters in the Web site, including a child element:
<session-timeout></session-timeout> to define the validity period for all sessions of this web site, in minutes


(2.7) defines an extension and a MIME type to be mirrored
[HTML] view plain copy <mime-mapping>    & nbsp;  <extension>doc</extension>  < the name of the!--extension-->        <mime-type>application/vnd.ms-word</mime-type>   <!--MIME Format-->    </mime-mapping>    <mime-mapping>        <extension>xls</extension>       <mime-type>application/ vnd.ms-excel</mime-type>   </mime-mapping>  
(2.8) Define the first page list (definition of the first page of the file name (can have multiple)) [HTML] view plain copy <welcome-file-list>       < welcome-file>index.jsp</welcome-file>         <welcome-file >index.html</welcome-file>   </welcom-file-list>  
(2.9) pages used to handle error codes or exceptions

3 child elements:<error-code></error-code> specified error code
                       <exception-type></exception-type> Specify a Java exception type
          & nbsp           <location></location> Specify the related resource path in the Web site [HTML] View Plain Copy <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>  
(2.10) Set the tag library path used for JSP Web pages

2 child elements:<taglib-uri></taglib-uri> defines the URI of the TLD file, which can be obtained by using the taglib instruction in the JSP Web page to obtain the TLD file of the URI
<taglib-location></taglib-location> specify where TLD files are stored relative to the Web site [HTML] view plain copy <taglib> <taglib-uri>myTaglib</taglib-uri> <taglib-location>/web-inf/tlds/mytaglib.tld</taglib-loca Tion> </taglib>

(2.11) Define the use of Jndi to obtain the resources available on the platform

Five child elements:
    <description></description> Resource Description
    < rec-ref-name></rec-ref-name> Resource Name
    <res-type></res-type> resource type
     <res-auth></res-auth> Resources licensed via application or container
    < Res-sharing-scope></res-sharing-scope> resources can be shared, there are shareable and unshareable two values, the default is shareable


To configure the database connection [HTML] view plain copy <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>  

(2.12) <jsp-config> configuration
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>: A description of the setting;
2.<DISPLAY-NAME>: Set the name;
3.<URL-PATTERN>: The range that the setting value affects, for example:/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 page, with the extension. JSPF;
8.<INCLUDE-CODA>: Sets the end of the JSP Web page with the extension. JSPF.

For example:[HTML] View plain copy <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>     


(iii) Configure Web.xml to restrict requests to certain servlet

If only authenticated users can request some servlet, they may be configured in Web.xml for this purpose.

Step One: Modify the/conf/tomcat-users.xml of the Tomcat server and set the username and password

Here's a fragment of Tomcat-users.xml.
[HTML] view plain copy <?xml version= ' 1.0 ' encoding= utf-8 '?> ' <tomcat-users

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.