The function and basic configuration of JSP Web.xml file _jsp programming

Source: Internet
Author: User
Tags error code error handling tomcat
There can be no web.xml files in a web, that is to say, web.xml files are not necessary for Web engineering.

When do you need it, and when do you want it?
To answer the question above, you need to know what the Web.xml file is for. Web.xml files are used to configure: Welcome pages, servlet, filter, and so on. When your Web project doesn't work, you can configure your Web project without web.xml files.
So what can web.xml do with all that stuff?
In fact, how many tag elements are defined in the Web.xml schema file, the tag elements defined by its schema file can appear in Web.xml, and it can have those defined functions. Web.xml's schema file is defined by Sun, and each web.xml file's root element <web-app> 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 the tags defined in the Web.xml schema file are not dead, schema files can also be changed, in general, with the version of the WEB.MXL mode file upgrade, the definition of the function will become more and more complex, that is, the label elements of the species will be more and more, but some are not commonly used, we just need to remember some commonly used on it.

The following list web.xml commonly used label elements and the functionality of these label elements:
1, the designated 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 2 welcome pages, which are displayed sequentially from the first, and if the first exists, the first is displayed, and the following does not work. If the first one doesn't exist, find the second one, and so on.
About the Welcome page:
When you visit a Web site, the first page that you see by default is called the Welcome page, which is generally used as a welcome page by the homepage. In general, we will specify the Welcome page in Web.xml. But Web.xml is not a necessary document for the Web, no web.xml, the website still can work normally. Only when the function of the website is complex, web.xml is very useful, so the dynamic Web project created by default has a Web.xml file under the Web-inf folder.
For Tomcat, when you specify only the root name of a web, without specifying a specific page, to access a web, If the Welcome page is configured in the Web.xml file, then return the specified page as a welcome page, and in the text there is no Web.xml file, or although there are web.xml, but Web.xml also did not specify the Welcome page, it defaults to find the index.html file, if found, the index.htm L return to the browser as Welcome page. If you don't find Index.html,tomcat, go find index.jsp. Find index.jsp and return it as a welcome page. If index.html and index.jsp are not found, and do not specify the Welcome page with Web.xml file, then Tomcat does not know which file to return, it shows the requested resource (/XXX) is not Available the page. where xxx represents the root name of the web. But if you specify a specific page, you can access it normally.
2, name and custom URL. We can name and customize the URLs for the servlet and JSP files, where the custom URLs are dependent on a named and must be named before the custom URL. Here's a serlet to give examples:
(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 servlet, JSP, the context of initialization parameters, and then the servlet, JSP, and then get these parameter values. Which of the following servlet is for 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>E-mail</param-name>
<param-value>Tommy@163.com</param-value>
</init-param>
</servlet>
The above configuration allows Getservletconfig (). Getinitparameter ("param1") to be invoked in the servlet to obtain the value of the parameter name.
4, specify the error handling page, you can specify the error handling page 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 the filter: For example, set a coded filter, 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 session expiration time, where time is in minutes, if set 60 Minutes timeout:
<session-config>
<session-timeout>60</session-timeout>
</session-config>
In addition to these tag elements, you can add those tag elements to the web.xml, what can those tag elements do? All we have to do is look at the web.xml pattern file to know. Directly look at the mode file can not understand, you may find some Chinese tutorials to see.
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.