Home>Article>Backend Development> Detailed introduction to the role of web.xml file

Detailed introduction to the role of web.xml file

黄舟
黄舟 Original
2017-03-11 18:03:28 2376browse

There is a web.xmlfile 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 adefinitionSchema 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. Theschemafile 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 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:

  index.jsp index1.jsp 

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:

 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   E-mail 125485762@qq.com  


4. Specify theerror handlingpage, which can be done through "Exception Type" or "Error Code" Specify error handling page.

 404 /error404.jsp  -----------------------------  java.lang.Exception /exception.jsp 


5. Setfilter: For example, set an encoding filter to filter all resources

 XXXCharaSetFilter net.test.CharSetFilter   XXXCharaSetFilter /* 


6. Set the listener:

 net.test.XXXLisenet 


7. Set the session (Session) expiration time, where the time is in minutes. If you set a 60-minute timeout:

 60 

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!

Statement:
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