Web.xml in the servlet, Filter, and listener configuration

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.

<web-app 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/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 your Web site as follows

<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

<env-entry>    
     <env-entry-name>minExemptions</env-entry-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>

<filter>
        <filter-name>setCharacterEncoding</filter-name>
        <filter-class> com.mytest.setcharacterencodingfilter</filter-class>
        <init-param>
            <param-name> encoding</param-name>
            <param-value>GB2312</param-value>
        </init-param>
< /filter>


<filter-mapping> to and <filter> matching use settings are shown below <filter>

<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)

<listener>
    <listener-class>com.myTest.ContextListener</listener-class> 
</listener >

(2.5) Servlet

Define servlet and servlet-mapping in the following way

<servlet>
        <servlet-name>ShoppingServlet</servlet-name>   <!--specify servlet name-->
        <servlet-class>com.myTest.ShoppingServlet</servlet-class>  <!--Specifies the servlet's class name (Implementation)-->
</servlet>
    
<servlet-mapping>
        <servlet-name>ShoppingServlet</servlet-name>
        <url-pattern>/shop/ShoppingServlet</url-pattern> <!--Specify the url--> </that the servlet corresponds to
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) Define an extension and a MIME type to do the mapping

<mime-mapping>
    <extension>doc</extension>  <!--extended name-->
    <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) Definition of the first page list (definition of the first page of the file name (can have multiple))

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

(2.9) a page for handling error codes or exceptions

3 child elements:<error-code></error-code> Specify error codes
<exception-type></exception-type> Specify a Java exception type
<location></location> specify the relevant resource path within the Web site

<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 by the JSP Web page

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 the TLD file is stored relative to the Web platform

<taglib>
      <taglib-uri>myTaglib</taglib-uri>
      <taglib-location>/web-inf/tlds/ Mytaglib.tld</taglib-location>
</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
Types of <res-type></res-type> Resources
<res-auth></res-auth> resources licensed through application or container
<res-sharing-scope></res-sharing-scope> resources can be shared, there are shareable and unshareable two values, default to shareable


You can configure the database connection here

<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:

<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.

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<tomcat-users>
  <role rolename= ' tomcat '/>
  <role rolename= "manager"/>
  <role rolename= "admin"/> <user username=
  "Tomcat" password= " Tomcat "roles=" Tomcat "/>
  <user username=" Both "password=" Tomcat "roles=" Tomcat,manager "/>
  < User username= "admin" password= "admin" roles= "admin"/>
</tomcat-users>

Step Two: Modify the web.xml of the Web application to create Security-constraint, Login-config, and security-role elements

<security-constraint>
      <web-resource-collection>
          <web-resource-name>helloservlet</ web-resource-name>
          <url-pattern>/HelloServlet</url-pattern>
          

Where: The Security-constraint element contains one or more web-resource-collection elements that describe which Web resources in the Web application are protected by the specified security restrictions. The Http-method element specifies the HTTP method that the security limit overrides. In the example above, the configured security is triggered when we apply a/helloservlet get or POST request.

The Auth-constraint element is used to describe security roles that allow access to Web components. Examples of security roles in this example are Tomcat, manager, admin. Only users who are in the admin role can access helloservlet.

The Web application authenticates the user by Login-config elements and confirms that the user is the correct role.
Longin-config contains transport-guarantee child elements used to specify authentication methods,Basic is a common Web authentication method, the browser to the user prompts a dialog box, asking for a username and password, Tomcat then compares the username and password given to the username and password in tomcat-users.xml, and then uses the previous Security-constraint configuration to determine whether the user has access to the protected servlet.

(In addition to basic, can also be form, Client-cert, digest, etc.)

Step three: When accessing the application in step two, the browser will ask for a username and password, enter the username and password set in the Tomcat configuration file


Verify the total steps

1, check the supplied username and password is correct.
2, to determine whether the user mapped to a specific security role. For example, a user may provide the correct user name and password, but not mapped to a specific security role, and will be prevented from accessing a particular Web resource.
3, Access, enter user name and password



(iv) Configuring EJB

EJB Declaration

<ejb-ref>    
     <description>example EJB reference</decription>    
     <ejb-ref-name>ejb/ account</ejb-ref-name>    
     <ejb-ref-type>Entity</ejb-ref-type>    
     

Local EJB Declaration

<ejb-local-ref>    
     <description>example loacal ejb reference</decription>    
     <ejb-ref-name >ejb/ProcessOrder</ejb-ref-name>    
     <ejb-ref-type>Session</ejb-ref-type>    
     < Local-home>com.mycompany.mypackage.processorderhome</local-home>    
     <local> Com.mycompany.mypackage.processorder</local>    
</ejb-local-ref>

(v) Configuration Dwr

<servlet>    
      <servlet-name>dwr-invoker</servlet-name>    
      <servlet-class> uk.ltd.getahead.dwr.dwrservlet</servlet-class>    
</servlet>    
<servlet-mapping>    
      <servlet-name>dwr-invoker</servlet-name>    
      <url-pattern>/dwr/*</url-pattern>    
</servlet-mapping>    

(vi) Configuration of struts

For example:

 <display-name>struts Blank application</display-name> <servlet> <servlet-name&gt    
        ;action</servlet-name> <servlet-class> Org.apache.struts.action.ActionServlet    
            </servlet-class> <init-param> <param-name>detail</param-name>    
            <param-value>2</param-value> </init-param> <init-param>    
        <param-name>debug</param-name> <param-value>2</param-value>    
            </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> ;init-param> <param-name>application</param-name> <param-value>applic Ationresources</param-value> </init-param> <load-on-startup>2</load-on-startup> &LT;/SERVL et> <servlet-mapping> <servlet-name>action</servlet-name> <url-pat tern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcom E-file>index.jsp</welcome-file> </welcome-file-list> <!--Struts Tag Library Descriptor s--> <taglib> <taglib-uri>struts-bean</taglib-uri> <taglib-locati On>/web-inf/tld/struts-bean.tld</taglib-location> </taglib> <taglib> < Taglib-uri>struts-html</taglib-uri> <taglib-location>/web-inf/tld/struts-html.tld</taglib-lo    
    cation> </taglib> <taglib> <taglib-uri>struts-nested</taglib-uri> <taglib-location>/web-inf/tld/struts-nested.tld</taglib-location> </taglib> <taglib> <ta Glib-uri>struts-logic</taglib-uri> <taglib-location>/web-inf/tld/struts-logic.tld</taglib-lo    
        cation> </taglib> <taglib> <taglib-uri>struts-tiles</taglib-uri>     <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location> </taglib>

(vii) Configuration spring (basically included in the Struts configuration sink)

<!--Specify the spring configuration file location-->    
   <context-param>    
      <param-name>contextconfiglocation</ param-name>    
      <param-value>    
       <!--load multiple spring configuration files-->    
        /web-inf/applicationcontext.xml,/ Web-inf/action-servlet.xml    
      </param-value>    
   </context-param>    

   <!--define spring listener, load spring -->    

<listener>    
     <listener-class>org.springframework.web.context.contextloaderlistener </listener-class>    
</listener>    

<listener>    
     <listener-class>    
       Org.springframework.web.context.request.RequestContextListener    
     </listener-class>    











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.