Functions and basic configurations of jsp web. xml files

Source: Internet
Author: User

A web project does not have a web. xml file. That is to say, the web. xml file is not required by the web project.

So when and when?
To answer the above question, you must first understand what the web. xml file is used. The web. xml file is used to configure the welcome page, servlet, filter, and so on. When your web project is useless, you can configure your web project without using the web. xml file.
So what does web. xml do?
In fact, web. how many tag elements are defined in the xml Schema file. the tag elements defined in the schema file can appear in xml, and it can have the defined functions. Web. the xml schema file is defined by Sun, and each web. the root element of the xml file <web-app> must indicate this web. which mode file is used for xml. 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://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
</Web-app>
And web. the labels defined in the xml schema file are not fixed, and the schema file can also be changed. Generally, with the web. the version upgrade of the mxl mode file will make the defined functions more and more complex, that is, there will be more and more types of tag elements, but some are not commonly used, we only need to remember some common ones.

The following lists tag elements commonly used in web. xml and their functions:
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>
In the preceding example, two welcome pages are specified. When displayed, the page starts from the first page in sequence. If the first page exists, the first page is displayed, and the subsequent page does not work. If the first one does not exist, find the second one, and so on.
Welcome Page:
When you access a website, the first page displayed by default is the welcome page. Generally, the homepage serves as the welcome page. Generally, the welcome page is specified in web. xml. However, web. xml is not a necessary Web file. Without web. xml, the website can still work normally. However, after the function of the website is complex, web. xml is indeed very useful, so the default dynamic web project created in the WEB-INF folder has a web. xml file.
For tomcat, When you specify only one web root name and no specific page, When you access a web page, the ghost will return to the browser as the welcome page. If index.html is not found, tomcat will go to index. jsp. Find index. jsp and return it as a welcome page. If index.html and index. jsp is not found, and web is not used. if The xml file specifies a welcome page, tomcat does not know which file to return, and The requested resource (/XXX) is not available page is displayed. XXX indicates the web root name. However, if you specify a specific page, you can access it normally.
2. Name and custom URL. We can name the Servlet and JSP files and customize the URLs. The custom URLs are named by one, and must be named before the custom URLs. The following uses serlet as an example:
(1) Name the Servlet:
<Servlet>
<Servlet-name> servlet1 </servlet-name>
<Servlet-class> net. test. TestServlet </servlet-class>
</Servlet>
(2) custom URL for Servlet,
<Servlet-mapping>
<Servlet-name> servlet1 </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>

3. Custom initialization parameters: You can customize the initialization parameters of servlet, JSP, and Context, and then obtain these parameter values in servlet, JSP, and Context. Which of the following servlets is used as an example:
<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> email </param-name>
<Param-value> Tommy@163.com </param-value>
</Init-param>
</Servlet>
After the above configuration, the servlet can call getServletConfig (). getInitParameter ("param1") to obtain the value corresponding to the parameter name.
4. Specify the error handling page. You can use "exception type" or "error code" to specify the 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 a 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, in minutes. For example, set the timeout time to 60 minutes:
<Session-config>
<Session-timeout> 60 </session-timeout>
</Session-config>
In addition to these tag elements, you can also add those tag elements to web. xml. What can these tag elements do? You only need to view the web. xml schema file. You cannot understand the mode file directly. You can find some Chinese tutorials.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.