The role of web.xml in web projects

There is a web.xml file in every javaEE project, so what is its role? 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 a web project. 
The web.xml file is used to initialize configuration information: such as Welcome page, servlet, servlet-mapping, filter, listener, startup loading level, etc.

When the web project does not use these, you can configure your Application without the web.xml file.

Each xml file has a Schema file that defines its writing rules, that is to say, how many tag elements are defined in the xml Schema file corresponding to the definition web.xml of javaEE, the tag elements defined by it can appear in web.xml , and what specific functions it has. The schema file of web.xml is defined by Sun Corporation. The root element of each web.xml file is <web-app>, which must be marked with which schema file the web.xml uses. Such as:

<?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> 

    The tags defined in the schema file of web.xml are not fixed, and the schema file can also be changed. Generally speaking, with the version upgrade of the web.mxl schema file, the functions defined in it will become more and more complex. There will definitely be more and more types.

Some commonly used tag elements and their functions in web.xml: 

1. Specify the welcome page.

E.g: 

<welcome-file-list> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>index1.jsp</welcome-file> 
  </welcome-file-list> 
PS:指定了2个欢迎页面,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推。 

About the welcome page: 
    When visiting a website, the first page you see by default is called the welcome page. Generally, the home page serves as the welcome page. Under normal circumstances, 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. It's just that after the function of the website is 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 custom URL.

    We can name and customize the URL for Servlet and JSP files, where the custom URL is named depending on the name, and the name must be before the custom URL. Let's take serlet as an example: 

(1)、为Servlet命名: 
<servlet> 
    <servlet-name>servlet1</servlet-name> 
    <servlet-class>org.whatisjava.TestServlet</servlet-class> 
</servlet> 

(2)、为Servlet定制URL、 
<servlet-mapping> 
    <servlet-name>servlet1</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping>

3. Customize initialization parameters.

    The initialization parameters of servlet, JSP, and Context can be customized, and then the parameter values ​​can be obtained from servlet, JSP, and Context. 
Here's an example with a servlet: 

<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>[email protected]</param-value> 
    </init-param> 
</servlet> 
经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。 

4. Specify the error handling page.

The error handling page can be specified by "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> 
    <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 expiration time.

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, many tag elements can be added to web.xml, which are omitted because they are not commonly used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324968057&siteId=291194637