Home>Article>Web Front-end> 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
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
index.html index.htm index.jsp default.html default.htm default.jsp
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:
servlet1 org.whatisjava.TestServlet
(2) Customize the URL for the Servlet
servlet1 *.do
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:
servlet1 org.whatisjava.TestServlet userName Daniel 125485762@qq.com
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".
----------------------------- 404 /error404.jsp java.lang.Exception /exception.jsp
5. Set the filter
For example, set an encoding filter to filter all resources
EncodingFilter encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding encoding UTF-8 encodingFilter /*
6. Set the listener
net.test.XXXLisenet
7. Set the session expiration time
The time is in minutes. If you set a 60-minute timeout:
60
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!