Detailed introduction to the role of web.xml file

黄舟
Release: 2017-03-11 18:03:28
Original
2481 people have browsed it

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 , which schema file is used by this web.xml must be indicated. For example:

<?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>
Copy after login

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>
Copy after login

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>
Copy after login

(2), customize the URL for the Servlet,

<servlet-mapping> 
    <servlet-name>servlet1</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping>
Copy after login



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>
Copy after login


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>
Copy after login


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>
Copy after login


6. Set the listener:

<listener> 
<listener-class>net.test.XXXLisenet</listener-class> 
</listener>
Copy after login


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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template