1. Common elements of web.xml configuration file and their meaning preview
<web-app>
<!--Defines the name of the WEB application-->
<display-name></display-name>
<!--Declare the description information of the WEB application-->
<description></description>
<!--context-param element declares application-scoped initialization parameters -->
<context-param></context-param>
<!--The filter element associates a name with a class that implements the javax.servlet.Filter interface -->
<filter></filter>
<!--Once a filter is named, associate it with one or more servlets or JSP pages using the filter-mapping element-->
<filter-mapping></filter-mapping>
<!--Version 2.3 of the servlet API added support for event listeners, which are notified when a session or servlet context is established, modified, and deleted.
The Listener element indicates the event listener class -->
<listener></listener>
<!--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></servlet>
<!--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 -->
<servlet-mapping></servlet-mapping>
<!--If a session is not accessed for a certain period of time, the server can discard it to save memory. Available by using HttpSession
The setMaxInactiveInterval method explicitly sets the timeout value for a single session object, or can use the session-config element to specify a default timeout value -->
<session-config></session-config>
<!--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-->
<mime-mapping></mime-mapping>
<!--Indicates which file the server uses when it receives a URL that refers to a directory name instead of a file name -->
<welcome-file-list></welcome-file-list>
<!--The ability to specify the page to be displayed when a specific HTTP status code is returned, or when a specific type of exception is thrown -->
<error-page></error-page>
<!-- Specify an alias for the Tag Libraryu Descriptor file. This feature enables you to change the location of the TLD file,
without editing the JSP pages that use these files -->
<taglib></taglib>
<!--Declare a management object related to the resource-->
<resource-env-ref></resource-env-ref>
<!--Declare an external resource used by a resource factory-->
<resource-ref></resource-ref>
<!--Make URLs that should be protected. It is used in conjunction with the login-config element -->
<security-constraint></security-constraint>
<!--Specifies how the server should authorize users trying to access protected pages. It is used in conjunction with the security-constraint element -->
<login-config></login-config>
<!-- 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></security-role>
<!--Declare the environment item of the web application-->
<env-entry></env-entry>
<!--Declare a reference to the EJB's home directory-->
<ejb-ref></ejb-ref>
<!--Declare an application of EJB's local home directory-->
<ejb-local-ref></ejb-local-ref>
</web-app>
2. Detailed explanation of each configuration element
1. Web Application Icons: Indicate 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>
2. Web Application Name: Provide a name that GUI tools may use to label this particular web application
<display-name>Tomcat Example</display-name>
3. Web Application Description: Give an explanatory text related to this
<desciption>Tomcat Example servlets and JSP pages.</desciption>
4. Context parameters: declare application-wide initialization parameters
<context-param>
<param-name>parameter name</para-name>
<param-value>parameter value</param-value>
<description>Parameter description</description>
</context-param>
In the servlet, it can be obtained by getServletContext().getInitParameter("context/param")
5. 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>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
7. Servlet configuration
<servlet>
<servlet-name>servlet名称</servlet-name>
<servlet-class>servlet class full path</servlet-class>
<init-param>
<param-name>parameter name</param-name>
<param-value>parameter value</param-value>
</init-param>
<run-as>
<description>Security role for anonymous access</description>
<role-name>tomcat</role-name>
</run-as>
<load-on-startup>Specifies the order in which servlets are loaded when the web application starts up</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet名称</servlet-name>
<url-pattern>map path</url-pattern>
</servlet-mapping>
8. Session timeout configuration (unit is minutes)
<session-config>
<session-timeout>120</session-timeout>
</session-config>
9. MIME type configuration
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
10. Specify the welcome file 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>
11. Configure error pages
(1). Configure error-page by error code
<!--Configured to jump to the error handling page NotFound.jsp--> when a 404 error occurs in the system
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
(2). Configure the error-page by the type of exception
<!--Configures to jump to the error handling page error.jsp--> when java.lang.NullException (ie null pointer exception) occurs in the system
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
12. 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 the development tool 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>
13. Resource management object configuration
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
14. 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>
15. Security Restrictions 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>
16. Login verification 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>
17. Security roles: The security-role element gives a list of security roles that will appear in the role-name sub-element 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>
18. Web environment parameters : The env-entry element declares the environment item 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>
19. 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>
20. 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>