The loading order in the web.xml file is : context-param -> listener -> filter -> servlet
<web-app> <display-name></display-name> defines the name of the WEB application <description></description> declares the description information of the WEB application <context-param></context-param> The context-param element declares application-wide initialization parameters. The <filter></filter> filter element associates a name with a class that implements the javax.servlet.Filter interface. <filter-mapping></filter-mapping> Once a filter is named, it is associated with one or more servlets or JSP pages using the filter-mapping element. Version 2.3 of the <listener></listener>servlet API added support for event listeners, which are notified when a session or servlet context is created, modified, and deleted. The Listener element indicates the event listener class. <servlet></servlet> When specifying initialization parameters or custom URLs to a servlet or JSP page, you must first name the servlet or JSP page. The Servlet element is used to accomplish this task. <servlet-mapping></servlet-mapping> The server generally provides a default URL for the servlet: http://host/webAppPrefix/servlet/ServletName. However, this URL is often changed so that the servlet can access initialization parameters or handle relative URLs more easily. When changing the default URL, use the servlet-mapping element. <session-config></session-config> If a session is not accessed for a certain period of time, the server can discard it to save memory. The timeout value for a single session object can be set explicitly by using the setMaxInactiveInterval method of HttpSession, or a default timeout value can be specified using the session-config element. <mime-mapping></mime-mapping> If a web application has specific files it wants to be guaranteed to be assigned a specific MIME type, the mime-mapping element provides this guarantee. <welcome-file-list></welcome-file-list> Instructs the server which file to use when it receives a URL that refers to a directory name instead of a file name. <error-page></error-page> can specify the page that will be displayed when a specific HTTP status code is returned, or when a specific type of exception is thrown. <taglib></taglib> Specifies an alias for the Tag Libraryu Descriptor file. This feature enables you to change the location of TLD files without editing the JSP pages that use these files. <resource-env-ref></resource-env-ref> declares a managed object associated with the resource. <resource-ref></resource-ref> declares an external resource used by a resource factory. <security-constraint></security-constraint> specifies the URLs that should be protected. It is used in conjunction with the login-config element <login-config></login-config> Specifies how the server should authorize users trying to access protected pages. It is used in conjunction with the security-constraint element. <security-role></security-role> gives a list of security roles that will appear in the role-name subelement of the security-role-ref element within the servlet element. Declaring roles separately makes it easier for advanced IDEs to handle security information. <env-entry></env-entry> declares the environment entry for the web application. <ejb-ref></ejb-ref> declares a reference to the EJB's home directory. <ejb-local-ref></ejb-local-ref> declares the application of an EJB's local home directory. </web-app>
Web Application Icons: Indicates the large and small icons that IDEs and GUI tools use to represent web applications
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>
Filter configuration: Associate 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>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6. Listener configuration
<listener>
<listerner-class>listener.SessionListener</listener-class>
</listener>
7. Servlet configuration
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>
</servlet-mapping>
Element Description
<servlet></servlet> is used to declare the data of a servlet, which mainly has the following sub-elements:
<servlet-name></servlet-name> specifies the name of the servlet
<servlet-class></servlet-class> specifies the class name of the servlet
<jsp-file></jsp-file> specifies the full path of a JSP page in the web site
<init-param></init-param> is used to define parameters, there can be multiple init-params. Access initialization parameters in the servlet class through the getInitParamenter(String name) method
<load-on-startup></load-on-startup> specifies the order in which servlets are loaded when the web application starts.
When the value is positive or zero: the servlet container loads the servlets with smaller values first, and then loads other servlets with larger values in turn.
When the value is negative or undefined: the servlet container will load this servlet the first time a web client accesses it
<servlet-mapping></servlet-mapping> is used to define the URL corresponding to the servlet and contains two sub-elements
<servlet-name></servlet-name> specifies the name of the servlet
<url-pattern></url-pattern> specifies the URL corresponding to the servlet
Session timeout configuration (in minutes)
<session-config>
<session-timeout>120</session-timeout>
</session-config>
MIME type configuration
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
Specify the welcome page configuration
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list>
configure error page
1. Configure error-page by error code
error-page> <error-code>404</error-code> <location>/NotFound.jsp</location> </error-page>
The above configuration is to jump to the error handling page NotFound.jsp when a 404 error occurs in the system.
2. Configure error-page by exception type
<error-page> <exception-type>java.lang.NullException</exception-type> <location>/error.jsp</location> </error-page>
The above configuration is to jump to the error handling page error.jsp when java.lang.NullException (that is, null pointer exception) occurs in the system
TLD configuration
<taglib> <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location> </taglib>
If MyEclipse keeps reporting errors, you should put < taglib > in < jsp - config >
<jsp-config> <taglib> <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location> </taglib> </jsp-config>
Resource management object configuration
<resource-env-ref> <resource-env-ref-name>jms/StockQueue</resource-env-ref-name> </resource-env-ref>
Resource Factory Configuration
<resource-ref> <res-ref-name>mail/Session</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth> </resource-ref>
Configuring the database connection pool can be configured 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>
Security Restriction Configuration
<security-constraint> <display-name>Example Security Constraint</display-name> <web-resource-collection> <web-resource-name>Protected Area</web-resource-name> <url-pattern>/jsp/security/protected/*</url-pattern> <http-method>DELETE</http-method> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>PUT</http-method> </web-resource-collection> <auth-constraint> <role-name>tomcat</role-name> <role-name>role1</role-name> </auth-constraint> </security-constraint>
Login authentication configuration
<login-config> <auth-method>FORM</auth-method> <realm-name>Example-Based Authentiation Area</realm-name> <form-login-config> <form-login-page>/jsp/security/protected/login.jsp</form-login-page> <form-error-page>/jsp/security/protected/error.jsp</form-error-page> </form-login-config> </login-config>
Security roles: The security-role element gives a list of security roles that will appear in the role-name subelement of the security-role-ref element within the servlet element.
Declaring roles separately makes it easier for advanced IDEs to handle security information.
<security-role> <role-name>tomcat</role-name> </security-role>
Web environment parameters: The env-entry element declares the environment items of the web application
<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>
EJB declaration
<ejb-ref> <description>Example EJB reference</decription> <ejb-ref-name>ejb/Account</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <home>com.mycompany.mypackage.AccountHome</home> <remote>com.mycompany.mypackage.Account</remote> </ejb-ref>
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>
Placement 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>
Configure Struts
<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>
Configure Spring (basically all configured in Struts)
<!-- Specify the location of the spring configuration file--> <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> </listener>