Structure of Web. xml

Source: Internet
Author: User

1. Define the header and root element
The deployment descriptor file, like all XML files, must start with an xml header. This header declares the XML version that can be used and provides the character encoding of the file.
The docytpe statement must appear immediately after this header. This Declaration tells the server the version of the applicable servlet specification (such as 2.2 or 2.3) and specifies the DTD (Document Type Definition, document type definition) that manages the syntax of the remaining part of the file ).
The top-level (Root) element of all deployment descriptor files is web-app. Note that XML elements are case sensitive, Unlike HTML. Therefore, web-app and WEB-APP are invalid, web-app must be in lower case.
2. Deploy the element order in the descriptor File
XML
Elements are not only case sensitive, but also order sensitive to other elements. For example, the xml header must be the first item in the file, the doctype Declaration must be the second item, and the Web-
The app element must be the third item. In web-app elements, the order of elements is also important. Servers do not necessarily require this order, but they allow (in fact some servers do this) completely
Deny the execution of Web applications that contain elements with incorrect order. This indicates that the Web. xml file with non-standard element order cannot be transplanted.
The following list shows the order of all valid elements that can directly appear in the Web-app element. For example, this list indicates that the servlet element must appear before all servlet-mapping elements. Note that all these elements are optional. Therefore, an element can be omitted, but it cannot be placed in an incorrect position.

Element
Description

Icon
The icon element indicates that IDE and GUI tools are used to represent the location of one or two image files of a Web application.

Display-name
The display-name element provides a GUI tool that may be used to mark the name of a specific web application.

Description
The description element provides descriptive text related to this.

Context-Param
The context-Param element declares the initialization parameters within the application range.

Filter
The filter element associates a name with a class that implements the javax. servlet. Filter interface.

Filter-Mapping
Once a filter is named, use the filter-mapping element to associate it with one or more servlet or JSP pages.

Listener
Servlet API Version 2.3 adds support for the event listening program. The event listening program is notified when the session or servlet environment is created, modified, and deleted. The listener element specifies the event listener class.

Servlet
When you specify initialization parameters or custom URLs to the servlet or JSP page, you must first name the servlet or JSP page. The servlet element is used to complete this task.

Servlet-Mapping
The server generally provides a default URL for servlet: http: // host/webappprefix/servlet/servletname
. However, this URL is often changed so that the servlet can access initialization parameters or process relative URLs more easily. When you change the default URL, use the servlet-mapping element.

Session-config
If a session is not accessed for a certain period of time, the server can discard it to save memory. You can use the setmaxinactiveinterval method of httpsession to explicitly set the timeout value for a single session object, or use the session-config element to specify the default timeout value.

Mime-Mapping
The mime-mapping element provides this guarantee if a web application wants to assign a specific MIME type to a specific file.

Welcom-file-list
The welcome-file-list element indicates which file the server uses when it receives a URL that references a directory name rather than a file name.

Error-page
The error-page element allows you to specify the page to be displayed when a specific HTTP status code is returned or a specific type of exception is thrown.

Taglib
The taglib element specifies an alias for the tag library descriptor file (TAG libraryu descriptor file. This feature allows you to change the location of TLD files without editing the JSP pages that use these files.

Resource-env-ref
The resource-env-ref element declares a resource-related management object.

Resource-ref
The resource-ref element declares the external resources used by a resource factory.

Security-Constraint
The security-constraint element specifies the URL to be protected. It is used together with the login-config element.

Login-config
Use the login-config element to specify how the server grants permissions to users trying to access protected pages. It is used with the sercurity-constraint element.

Security-Role
The security-role element provides a list of security roles that appear in the role-name sub-element of the security-role-ref element in the servlet element. Declaring roles separately makes it easier for advanced ide to process security information.

Env-entry
The env-entry element declares the environment items of the Web application.

EJB-ref
The EJB-ref element declares a reference to the main directory of an EJB.

EJB-local-ref
The EJB-local-ref element declares the application of an EJB local main directory.

3. Name allocation and customized ul
One of the most common tasks completed in Web. XML is to give the servlet or JSP page names and custom URLs. Use the servlet element to assign a name, and use the servlet-mapping element to associate the custom URL with the name you just allocated.

3.1 allocation name
To provide initialization parameters, define a custom URL for the servlet or JSP page or assign a security role to the Servlet
Or a JSP page name. You can assign a name through the servlet element. The most common formats include servlet-Name and servlet-class sub-elements (in
In the web-app element), as follows:
<Servlet>
<Servlet-Name> test </servlet-Name>
<Servlet-class> moreservlets. testservlet </servlet-class>
</Servlet>

This indicates that the servlet located in the WEB-INF/classes/moreservlets/testservlet has obtained the registration name test. A name for the servlet has two main meanings.

First, initialize the parameter, customize the URL mode, and use this registration name instead of the class name to reference this servlet.

Second, you can use this name in the URL rather than in the class name. Therefore, the URL http: // host/webappprefix/servlet/test
It can be used for http: // host/webappprefix/servlet/moreservlets. testservlet
.
 
Remember: XML elements are not only case sensitive, but also important to define their order. For example, all servlet elements in the Web-app element must be located in all servlet-
Before the ing element (introduced in the next section), it is also located before the filter or document-related elements discussed in sections 5.6 and 5.11 (if any. Similarly, Servlet
The servlet-name sub-element must also appear before the servlet-class. Section 5.2 "sequence of elements in the deployment descriptor file" describes the required sequence in detail.
 
For example, the program listing 5-1 provides a simple servlet named testservlet, which resides in the moreservlets package. Because this servlet is
The components of the Web application rooted in a directory named deploydemo, so testservlet. Class is placed in deploydemo/WEB-INF
/Classes/moreservlets.

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.