What's the use of Web. xml? (Java framework)

Source: Internet
Author: User

1. What is the role of the Web. xml file in each Java EE project? Is it necessary for every web. XML project?

A web can have no Web. xml file, which means that the. xml file is not required by the website.

The Web. xml file is used to initialize configuration information such as Welcome page, servlet, servlet-mapping, filter, listener, boot load level, and so on.

When your Web project doesn't work, you can configure your application without using the. xml file.

Each XML file has a schema file that defines its writing rules, meaning that Java EE defines how many tag elements are defined in the XML schema file for the definition of Web. Web, and the tag elements that it defines can appear in XML, as well as specific functions. The schema file for Web. XML is defined by sun, and the root element of each Web. xml file is <web-app>, which must indicate which schema file this Web. XML uses. 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>

The tags defined in the schema file of Web. xml are not dead, schema files can be changed, generally speaking, as the version of the WEB.MXL schema file is upgraded, the functions defined inside will become more and more complex, and the types of tag elements will certainly be more and more, but some are not very common, We just need to remember some common ones and know how to configure them.


Below is a list of some of the tag elements and their functions that are commonly used by web.

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>
PS: 2 Welcome pages are specified, displayed sequentially from the first, if the first exists, the first one is displayed, and the latter 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 you see by default is called a Welcome page, which in general is a welcome page by the homepage. In general, we will specify the Welcome page in Web. Xml. However, Web. XML is not a necessary document for the Internet, and the Web site is still functional without XML. However, the Web site has a very complex function, and Web. XML does have a great use, so the default creation of the dynamic website project is under the Web-inf folder with an Web. xml file.


  2, name and custom URL. We can name and customize the URL for the servlet and JSP files, where the custom URL is a dependent name, and the name must be before the custom URL. Take Serlet below for example:
(1), name the servlet:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.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, context initialization parameters, and then the servlet, JSP, context to get these parameter values.

Here's an example with a servlet:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
<init-param>
<param-name>userName</param-name>
<param-value>Daniel</param-value>
</init-param>
<init-param>
<param-name>E-mail</param-name>
<param-value>[email protected]</param-value>
</init-param>
</servlet>
With the above configuration, Getservletconfig () can be called in the servlet (). Getinitparameter ("param1") gets the value corresponding to the parameter name.



4. Specify error handling page, you can specify 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 up an encoding 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 a lot of tag elements to Web. XML, and later update ...

What's the use of Web. xml? (Java framework)

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.