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.