Home >Web Front-end >Front-end Q&A >What does web.xml do?
The functions of web.xml are: 1. It can be used to initialize configuration information such as the welcome page; 2. Naming and customizing URLs; 3. Customizing initialization parameters; 4. Specifying error handling pages; 5. Setting filters wait.
【Recommended course: Java Tutorial】
There is a web.xml file in every javaEE project, so what is its function? Is it required for every web.xml project? There can be no web.xml file in a web. In other words, the web.xml file is not necessary for web projects. The web.xml file is used to initialize configuration information: such as Welcome page, servlet, servlet-mapping, filter, listener, startup loading level, etc.
Each xml file has a Schema file that defines its writing rules. In other words, as many tag elements are defined in the xml Schema file corresponding to javaEE's definition web.xml, they can appear in web.xml The tag elements it defines also have specific functions. The schema file of web.xml is defined by Sun. The root element of each web.xml file is 8459cedd22f378aa35db2cd2b63decac, which schema file is used by this web.xml must be indicated. For example: The tags defined in the pattern file of
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>
web.xml
are not fixed, and the pattern file can also be changed. Generally speaking, as the web.mxl pattern file changes As the version upgrades, the functions defined in it will become more and more complex, and the types of label elements will definitely increase, but some are not very commonly used. We only need to remember some commonly used ones and know how to configure them.
The following lists some commonly used tag elements and their functions in web.xml:
1. Specify the welcome page
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
Note: Two welcome pages are specified. When displaying, start from the first one in order. If the first one exists, the first one will be displayed, and the following ones will not work. If the first one doesn't exist, find the second one, and so on.
2. Naming and customizing URLs
We can name and customize URLs for Servlet and JSP files. Customized URLs depend on naming, and naming must precede the customized URL. . Let’s take serlet as an example:
(1) Name the Servlet:
<servlet> <servlet-name>servlet1</servlet-name> <servlet-class>org.whatisjava.TestServlet</servlet-class> </servlet>
(2) Customize the URL for the Servlet
<servlet-mapping> <servlet-name>servlet1</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3. Customize initialization parameters
You can customize the initialization parameters of servlet, JSP, and Context, and then obtain these parameter values in servlet, JSP, and Context.
The following uses servlet as an example:
<servlet> <servlet-name>servlet1</servlet-name> <servlet-class>org.whatisjava.TestServlet</servlet-class> <init-param> <param-name>userName</param-name> <param-value>Daniel</param-value> </init-param> <init-param> <param-name>E-mail</param-name> <param-value>125485762@qq.com</param-value> </init-param> </servlet>
After the above configuration, you can call getServletConfig().getInitParameter("param1") in the servlet to obtain the value corresponding to the parameter name.
4. Specify the error handling page
You can specify the error handling page through "Exception Type" or "Error Code".
<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>
5. Set the filter
For example, set an encoding filter to filter all resources
<filter> <description>EncodingFilter</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>encoding</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
6. Set the listener
<listener> <listener-class>net.test.XXXLisenet</listener-class> </listener>
7. Set the session expiration time
The time is in minutes. If you set a 60-minute timeout:
<session-config> <session-timeout>60</session-timeout> </session-config>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone.
The above is the detailed content of What does web.xml do?. For more information, please follow other related articles on the PHP Chinese website!