jsp web.xml file function and basic configuration

  • 2020-05-07 20:14:08
  • OfStack

It is perfectly possible to have no web.xml file in 1 web, that is, web.xml file is not required for the web project.

So when do we need it and when can we not?
To answer these questions, you need to know what the web.xml file is used for. The web.xml file is used to configure the welcome page, servlet, filter, and so on. When your web project doesn't use these, you can configure your web project without the web.xml file.
So what are all the things that web.xml can do?
In fact, how many tag elements are defined in web.xml's schema file (Schema), web.xml's schema file defines tag elements, and it can have those functions defined. The schema file for web.xml is defined by Sun, the root element of each web.xml file < web-app > , you must indicate which schema file the web.xml is using. 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 >
And web. xml schema file defined in the tag is not set, also can change the schema file, 1 a, as web. mxl schema file version upgrade, which defined function will be more and more complex, also namely tag element types will be more and more, but some of them are not commonly used, we just remember some commonly used 1 is ok.

The following is a list of common web.xml tag elements and the functions of these tag elements:
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 >
The above example specifies two welcome pages, starting with the first one in order, and showing the first one if the first one exists. If the first one doesn't exist, find the second one, and so on.
About the welcome page:
When visiting a website, the default to see the first page is called welcome page, 1 generally by the home page as a welcome page. 1 in general, we specify the welcome page in web.xml. However, web.xml is not a necessary document for an Web. Without web. However, es1010en.xml is very useful after the complexity of the website. Therefore, the dynamic web project created by default has an web.xml file under the WEB-INF folder.
For tomcat, when you only specify one web root name, do not specify a specific page, when I went to visit a web, if web xml file configured in the welcome page, then returns to the specified page as a "welcome page, and in the article is not web. xml file, or despite web. xml, but web xml didn't specify the welcome page of the case, its default search index first. html file, If found, return index.html as the welcome page to the browser. If index.html cannot be found, tomcat goes to index.jsp. Find index.jsp and return it as a welcome page. If index.html and index.jsp are not found and the welcome page is not specified in the web.xml file, then tomcat does not know which file to return and it displays The requested resource (/XXX) is not available page. Where XXX represents the root name of web. But if you specify a specific page, it can be accessed normally.
2. Name and customize URL. We can name and customize URL for Servlet and JSP files, where the custom URL is named by 1, and the name must precede the custom URL. Here's an example of serlet:
(1) name Servlet:
< servlet >
< servlet-name > servlet1 < /servlet-name >
< servlet-class > net.test.TestServlet < /servlet-class >
< /servlet >
(2) customize URL for 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 the values of these parameters in servlet, JSP and Context. Here's an example of servlet:
< servlet >
< servlet-name > servlet1 < /servlet-name >
< servlet-class > net.test.TestServlet < /servlet-class >
< init-param >
< param-name > userName < /param-name >
< param-value > Tommy < /param-value >
< /init-param >
< init-param >
< param-name > E-mail < /param-name >
< param-value > Tommy@163.com < /param-value >
< /init-param >
< /servlet >
With the above configuration, getServletConfig().getInitParameter ("param1") can be invoked in servlet to obtain the value corresponding to the parameter name.
4. 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 filters: for example, set a coding 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 listener:
< listener >
< listener-class > net.test.XXXLisenet < /listener-class >
< /listener >
7. Set the expiration time of the session (Session), in which the time is measured in minutes. If set the timeout time of 60 minutes:
< session-config >
< session-timeout > 60 < /session-timeout >
< /session-config >
In addition to these tag elements, what other tag elements can you add to web.xml? What do those tag elements do? We just need to look at the web.xml schema file to find out. See the mode file directly do not understand, you can find 1 Chinese tutorial to see.

Related articles: