(1) Loading Sequence of different elements in Web. xml
The order of loading is irrelevant to their order in the web. xml file. That is, the filter will not be loaded first because the filter is written before the listener.
Web. the XML loading sequence is: servletcontext-> context-param-> listener-> filter-> servlet, the actual Program Calling sequence between the same types is called according to the corresponding Mapping Sequence.
(2) Explanation of the web. xml file
(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 stored in <web-app> </Web-app>.
<Discription> </discription> is the description of the site.
<Display-Name> </display-Name> defines the site name.
<Distributable> </distributable> specifies whether the platform can be processed in a distributed manner.
(2.2)
Set the environment parameters of the web site as follows:
<context-param> <param-name>my_param</param-name> <param-value>hello</param-value></context-param>
You can set the environment variables as follows:
<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 setting this parameter my_param, you can use getservletcontext (). getinitparameter ("my_param") in servlet to obtain the parameter.
(2.3) Filter
You can set filter as follows: <filter-Name>, <filter-class>, and <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> The following settings must be used with <filter> after <filter>:
<filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
(2.4) Listener listener
Set the listener interface as follows (the main sub-element <listener-class> </listener-class> defines the class name of listener.
<listener> <listener-class>com.myTest.ContextListener</listener-class> </listener>
(1, 2.5) Servlet
Define Servlet and servlet-mapping as follows
<Servlet> <servlet-Name> shoppingservlet </servlet-Name> <! -- Specify the servlet name --> <servlet-class> com. mytest. shoppingservlet </servlet-class> <! -- Specify the servlet Class Name (Implementation) --> </servlet> <servlet-mapping> <servlet-Name> shoppingservlet </servlet-Name> <URL-pattern>/shop/shoppingservlet </url-pattern> <! -- Specify the URL of the servlet --> </servlet-mapping>
(2.6) define session parameters
<Session-config> </session-config> is used to define the session parameters in the Web platform. It contains a sub-element:
<Session-Timeout> </session-Timeout> defines the validity period of all sessions on the web platform. The unit is (minutes)
(2.7) define a certain extension and a mime type for ing
<Mime-mapping> <extension> doc </extension> <! -- Extended name --> <mime-type> application/vnd. MS-word </mime-type> <! -- MIME format --> </mime-mapping> <extension> XLS </extension> <mime-type> application/vnd. MS-Excel </mime-type> </mime-mapping>
(2.8) define the Home Page List (define the file names of the home page (multiple files are allowed ))
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file></welcom-file-list>
(2.9) page used to handle error code or exceptions
Three sub-elements: <error-code> </error-code> specifies the error code.
<Exception-type> </exception-type> specifies a Java exception type.
<Location> </location> specifies the relevant resource path on the web platform.
<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 JSP web pages
Two sub-elements: <taglib-Uri> </taglib-Uri> defines the URI of the TLD file. You can use the taglib command on the JSP page to obtain the TLD file of the URI.
<Taglib-location> </taglib-location> specifies the storage location of TLD files 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 available resources on the site
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 are licensed by application or container.
<Res-sharing-scope> </RES-sharing-scope> whether resources can be shared. There are two values: retriable and unretriable. The default value is retriable.
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
The sub-elements include <taglib> and <JSP-property-group>.
The <taglib> element already exists in JSP 1.2, and <JSP-property-group> is a new element in JSP 2.0.
The <JSP-property-group> elements mainly have eight child elements:
1. <description>: Set description;
2. <display-Name>: Set the name;
3. <URL-pattern>: Set the range affected by the value, for example,/CH2 or/*. jsp;
4. <El-ignored>: if it is true, El syntax is not supported;
5. <Scripting-invalid>: if it is true, the <% scripting %> syntax is not supported;
6. <page-encoding>: sets the JSP page encoding;
7. <include-prelude>: Set the JSP page header with the extension. jspf;
8. <include-coda>: set the end of the JSP 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>
(3) Configure web. XML to restrict requests to some servlets
If you only want authenticated users to request some Servlets, you can configure them in Web. xml.
Step 1: Modify the/CONF/tomcat-users.xml of the Tomcat server and set the user name and password
The following is a snippet of the 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 2: Modify the Web. xml of the Web application and 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>
The security-constraint element contains one or more web-resource-collection elements, which describe which Web Resources in web applications are protected by specified security restrictions. The HTTP-method element specifies the HTTP Method covered by the security restriction. In the above example, when we get or POST requests to/helloservlet, the configured security mechanism is triggered.
The Auth-constraint element describes the security roles that allow access to Web components. In this example, the security roles include tomcat, manager, and Admin. Helloservlet can be accessed only when the user acts as the admin role.
Web applications use the login-config element to authenticate users and check whether the user is a correct role.
The transport-guarantee sub-element in Longin-config is used to specify the authentication method,Basic is a common Web authentication method.The browser prompts the user a dialog box asking to enter the user name and password, then Tomcat will give the user name and password and the user name and password in the tomcat-users.xml for comparison, then, use the preceding security-constraint configuration to determine whether the user can access the protected servlet.
(In addition to basic, it can also be form, client-cert, digest, etc)
Step 3: when accessing the application in step 2, the browser requires you to enter the user name and password, and enter the user name and password set in the tomcat configuration file.
Verification steps
1. Check whether the provided user name and password are correct.
2. Determine whether the user maps to a specific security role. For example, a user may provide the correct user name and password, but it is not mapped to a specific security role, and access to a specific web resource is also prohibited.
3. Access and enter the user name and password
(4) Configure 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>
(5) Configure 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>
(6) Configure struts
For example:
<display-name>Struts Blank Application</display-name> <servlet> <servlet-name>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>ApplicationResources</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>struts-bean</taglib-uri> <taglib-location>/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-location> </taglib> <taglib> <taglib-uri>struts-nested</taglib-uri> <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location> </taglib> <taglib> <taglib-uri>struts-logic</taglib-uri> <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>struts-tiles</taglib-uri> <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location> </taglib>
(7) Configure spring (basically included in struts configuration sink)
<! -- Specify the location of the spring configuration file --> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value> <! -- Load Multiple Spring profiles -->/WEB-INF/applicationcontext. XML,/WEB-INF/action-servlet.xml </param-value> </context-param> <! -- Define the spring listener and load spring --> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> <listener-class> Org. springframework. web. context. request. requestcontextlistener </listener-class> </listener>