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, that is to say, 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.
When your web project does not use these, you can configure your Application without the web.xml file.
Each xml file has a definition Schema file in which it writes rules, that is to say, how many tags are defined in the xml Schema file corresponding to javaEE's definition web.xml element, the tag element it defines can appear in web.xml, and it also has specific functions. The schema file of web.xml is defined by Sun. The root element of each web.xml file is
<?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://www.php.cn/"> </web-app>
The tags defined in the web.xml pattern file are not fixed, and the pattern file can also be changed. Generally speaking, as the version of the web.mxl pattern file is upgraded, 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, for example:
<welcome-file-list> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index1.jsp</welcome-file> </welcome-file-list>
PS: 2 welcome pages are specified Pages are displayed in order from the first one. If the first one exists, the first one will be displayed, and the following ones will have no effect. If the first one doesn't exist, find the second one, and so on.
About the welcome page:
When you visit a website, the first page you see by default is called the welcome page. Generally, the home page serves as the welcome page. Normally, we will specify the welcome page in web.xml. But web.xml is not a necessary file for the Web. Without web.xml, the website can still work normally. However, when the functions of the website become more complicated, web.xml is indeed very useful. Therefore, the dynamic web project created by default has a web.xml file under the WEB-INF folder.
2. Naming and customizing the URL. 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>
4. Specify the error handling page, which can be done through "Exception Type" or "Error Code" Specify error handling page.
<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 filter: For example, set an encoding filter to filter all resources
<filter> <filter-name>XXXCharaSetFilter</filter-name> <filter-class>net.test.CharSetFilter</filter-class> </filter> <filter-mapping> <filter-name>XXXCharaSetFilter</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 (Session) expiration time, where the time is in minutes. If you set a 60-minute timeout:
<session-config> <session-timeout>60</session-timeout> </session-config>
In addition to these tag elements, you can also add many tag elements to web.xml, which are omitted because they are not commonly used.
The above is the detailed content of Detailed introduction to the role of web.xml file. For more information, please follow other related articles on the PHP Chinese website!