Web. XML configuration Detailed

Source: Internet
Author: User
Tags event listener

NOTE *: Integrated development environment (ide,integrated development environment) is an application for the provider development environment

Note *: The GUI (graphical user Interface, or GUI, also known as graphical user interface) refers to the graphical display of the computer operating user interface.

Note *:EJB is the Sun's Java EE server-side component model, and the design goal and core application is to deploy distributed applications.

--------------------------------------------------------------------------------------------------------------- ----------------

1 defining headers and root elements

  

<? XML version= "1.0" encoding= "UTF-8" ?>

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 gives the character encoding of the file.

<xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"         xmlns = "Http://java.sun.com/xml/ns/javaee"           xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ Web-app_3_0.xsd "          ID=" webapp_id "  version=" 3.0 "> 

The DOCYTPE declaration must immediately appear after this header. This statement tells the server which version of the servlet specification is applicable (for example, 2.2 or 2.3) and specifies a DTD (document type definition, doc-types definitions) that manages the syntax for the remainder of this file.
The top-level (root) element of all deployment descriptor files is Web-app. Note that XML elements are not like HTML, and they are case-sensitive. Therefore, Web-app and Web-app are not legal, web-app must be lowercase.

2 element order within the deployment descriptor file

XML elements are not only case-sensitive, but they are also sensitive to the order in which they appear in 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-app element must be the third item. Within the Web-app element, the order of the elements is also important. Servers do not necessarily enforce this order, but they allow (in fact some servers do) a complete denial of the execution of Web applications that contain elements that are not in the correct order. This means that the Web. xml file using the non-standard element order is not portable.

The following list gives all the necessary order for the legitimate elements to appear directly within the Web-app element. For example, this list illustrates that a servlet element must appear before all servlet-mapping elements. Please note that all of these elements are optional. Therefore, you can omit an element, but you cannot put it in an incorrect position.

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">             <!--the icon element indicates the location of the IDE and GUI tools used to represent one and two image files of the Web App.  -    <icon></icon>        <!--The display-name element provides a name that GUI tools may use to mark this particular Web app.  -    <Display-name></Display-name>        <!--The description element gives explanatory text related to this.  -    <Description></Description>        <!--context-param Element declaration application-scoped initialization Parameters -    <Context-param></Context-param>        <!--The filter element associates a name with a class that implements the Javax.servlet.Filter interface.  -    <Filter></Filter>        <!--Filter-mapping Once a filter is named, use the filter-mapping element to associate it with one or more servlet or JSP pages.  -    <filter-mapping></filter-mapping>        <!--Listener support for event listeners, event listeners are notified when a session or servlet environment is established, modified, and deleted. The listener element indicates the event listener class.  -    <Listener></Listener>        <!--the servlet must first name the servlet or JSP page when it formulates initialization parameters or custom URLs to a servlet or JSP page. The servlet element is used to accomplish this task.  -    <servlet></servlet>        <!--The servlet-mapping server typically provides a default url:http://host/webappprefix/servlet/servletname for Servlets. However, this URL is often changed so that the servlet can access the initialization parameters or make it easier to handle relative URLs. Use the servlet-mapping element when changing the default URL.  -    <servlet-mapping></servlet-mapping>        <!--Session-config If a session is not accessed for a certain amount of time, the server can discard it to save memory. You can explicitly set a time-out value for a single Session object by using the Setmaxinactiveinterval method of HttpSession, or you can use the Session-config element to make a default timeout.  -    <Session-config></Session-config>        <!--mime-mapping The Mime-mapping element provides this assurance if the Web application has a special file that you want to guarantee to assign to them a specific MIME type.  -    <mime-mapping></mime-mapping>        <!--The welcome-file-list element indicates which file the server uses when it receives a URL that references a directory name instead of a file name.  -    <welcome-file-list></welcome-file-list>        <!--The error-page element enables the page to be displayed when a specific HTTP status code is returned, or when a particular type of exception is thrown.  -    <Error-page></Error-page>        <!--the Resource-env-ref element declares a management object that is associated with a resource.  -    <Resource-env-ref></Resource-env-ref>        <!--the Resource-ref element declares an external resource used by a resource factory.  -    <Resource-ref></Resource-ref>        <!--the Security-constraint element formulates the URL that should be protected. It is used in conjunction with the Login-config element -    <Security-constraint></Security-constraint>        <!--Use the Login-config element to specify how the server should authorize users attempting to access a protected page. It is used in conjunction with the Sercurity-constraint element.  -    <Login-config></Login-config>    <!--The security-role element gives a list of security roles that will appear in the Role-name child elements of the security-role-ref element within the servlet element. Declaring roles separately makes it easier for advanced Ides to handle security information.  -    <Security-role></Security-role>    <!--the Env-entry element declares the environment item for the web App.  -    <Env-entry></Env-entry>        <!--the Ejb-ref element declares a reference to the home directory of an EJB.  -    <Ejb-ref></Ejb-ref>        <!--the Ejb-local-ref element declares the application of an EJB's local home directory.  -    <Ejb-local-ref></Ejb-local-ref></Web-app>

3 Assigning a name and a custom URL

One of the most common tasks done in Web. XML is to give a name and a custom URL to a servlet or JSP page. Assign a name with the servlet element and use the servlet-mapping element to associate the custom URL with the name you just assigned.

3.1 Assigning names
To provide initialization parameters, define a custom URL for a servlet or JSP page, or assign a security role, you must first give the servlet or JSP page a name. A name can be assigned through the servlet element. The most common formats include servlet-name and Servlet-class child elements

    <servlet>        <Servlet-name>Servletname</Servlet-name>        <Servlet-class>Servletclass</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Servletname</Servlet-name>        <Url-pattern>Url</Url-pattern>    </servlet-mapping>

This means that the servlet at Web-inf/classes/servletclass has been registered with the name Servletname. Giving the servlet a name has two main meanings. First, initialize the parameters. Custom URL patterns and other customizations refer to this servlet through this registration name instead of the class name. Second, you can use this name in a URL instead of a class name. Thus, using the definition just given, URL http://host/webAppPrefix/servlet/ServletName can be used for http://host/webAppPrefix/servlet/ a place to Servletclass.

3.2 Defining a Custom URL

    

La La la

Web. XML configuration Detailed

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.