"Java Web" web.xml detailed __HTML5

Source: Internet
Author: User

The role of Web.xml

Web.xml, the most important configuration file in a Tomcat project. Web.xml not in fact can also----as long as you are sure that your project does not need any filters, listeners, servlet and so on. I tried, no web.xml. For those JSP pages that have been compiled into a servlet, it does not affect the normal display, but the JSP pages that have not been compiled into a servlet will report a 500 error when they visit. Below a look at the web.xml of the role of commonly used tags.

Welcome-file-list

This tag is used to configure the home page:

<welcome-file-list>
    <welcome-file>index1.jsp</welcome-file>
    <welcome-file> index2.jsp</welcome-file>
    <welcome-file>index3.jsp</welcome-file>
    <welcome-file >index4.jsp</welcome-file>
    <welcome-file>/target/redirectAndFoward.jsp</welcome-file>
</welcome-file-list>

This configuration means that when users visit the http://ip:port/project name, according to the Welcome-file-list configured page list, from the root directory of the project to start looking for pages:

1, the first configuration of the index1.jsp can be found, on the display index1.jsp

2, find index1.jsp, then find a second index2.jsp,index2.jsp can find on the show index2.jsp,

3, can not find index3.jsp, then to find a third index3.jsp, and so on, if all of the pages are not found, the report HTTP Status 404 that the page is not found

Notice that, like the last welcome-file of configuration, this writing is also supported, I tried the front of the "/" can add can not add

Error-page

Error-page indicates that when HTTP returns a specified status code, the container forwards this request to the specified page of the configuration:

<error-page>
    <error-code>400</error-code>
    <location>/filter/error.jsp</ location>
</error-page>
  
<error-page>
    <error-code>404</error-code>
    <location>/filter/error.jsp</location>
</error-page>
  
<error-page>
    < error-code>500</error-code>
    <location>/filter/error.jsp</location>
</ Error-page>

This means that the HTTP status code is 400, 404, 500, the request will be forwarded to the http://ip:port/project name/filter/error.jsp this page up. Notice this is error-code, so if it's 200, it's not working.

Filter

Filter does not say, the two include methods and filter in the dispatcher interpretation of the article has been very detailed, the method of filter is also above.

Another note, in fact, we all know, mention: Filter execution order is the order of the filter definition.

Servlet

Servlet developers are more familiar with the first match rules, matching to the path after the corresponding servlet class, do not say. The following is a relatively less common, but relatively, servlet notation is common:

<servlet>
    <servlet-name>startUpServlet</servlet-name>
    <servlet-class> com.xrq.servlet.startupservlet</servlet-class>
    <init-param>
        <param-name>name</ param-name>
        <param-value>123</param-value>
    </init-param>
    <init-param>
        <param-name>Age</param-name>
        <param-value>456</param-value>
    </ init-param>
    <load-on-startup>8</load-on-startup>
</servlet>

This is a boot servlet, which means that the servlet starts when the container is started and calls its init () method, so first the first label Load-on-start, a few points to say:

1. load-on-startup element tag container to load this servlet at startup (instantiate and invoke its Init method)

2, its value must be an integer, indicating the order in which the servlet should be loaded

3. A value of 0 or greater than 0 o'clock indicates that the container loads and initializes the servlet at the start of the application

4. When the value is less than 0 or unspecified, it means that the container is loaded when the servlet is selected

5, the smaller the positive value, the higher the priority of the servlet, the application started the more first load

6. At the same time, the container chooses the order to load

So, when a positive integer greater than or equal to 0 is configured in Load-on-startup, the servlet can be used as a normal servlet, but it will load its init () method when the servlet is started.

The other one is init-param, which means a key value pair, which can only be used in this servlet, through ServletConfig acquisition, Startupservlet is:

public class Startupservlet extends HttpServlet
{
    /**
     * Serialization/
    private static final long Serialversionuid = 1L;
    
    public void Init () throws Servletexception
    {
        System.out.println ("Startupservlet.init ()");
        System.out.println ("Name:" + getservletconfig (). Getinitparameter ("name"));
        System.out.println ("Age:" + getservletconfig (). Getinitparameter ("Age"));
    
    protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
            throws Servletexception, IOException
    {
        
    } public
    
    void Destroy ()
    {
        System.out.println ("Startupservlet.destory ()");
    }

The servlet can only take the <init-param> that is configured within its own <servlet> label.

Listener

Listener is the listener, is the servlet listener, it can monitor the client's request, server-side operations, etc., before and after the event to do some necessary processing. Through the listener, can automatically inspire some operations, such as listening to the number of online users, the following write a listener to monitor the number of users, first Web.xml configuration is very simple:

<listener>
    <listener-class>com.xrq.listener.UserCounterListener</listener-class>
</listener>

Write a listener, monitor the number of users are generally based on session creation and session failure, so realize Httpsessionlistener:

public class Usercounterlistener implements Httpsessionlistener
{
    private atomicinteger ai = new Atomicinteger ( 0);
    
    public void sessioncreated (httpsessionevent se)
    {
        ai.incrementandget ();
    }
    
    public void sessiondestroyed (httpsessionevent se)
    {
        ai.decrementandget ();
    }
    
    public int Getusercount ()
    {return
        ai.get ();
    }
}

In addition to listening to the session listener, introduce some other listener interfaces:

1, Servletcontextlistener

Used to listen for Web reference startup and destruction events, Sevletcontextlistener is a ServletContext listener, if servletcontext changes, such as server startup, server shutdown, Will be heard by Servletcontextlistener. Listener event for Servletcontextevent

2, Servletcontextattributelistener

An event that listens for changes to Web application properties, including adding properties, deleting attributes, and modifying properties. The listening time is servletcontextattributeevent

3, Httpsessionbindinglistener

Httpsessionbindinglistener is the only listener that does not need to be configured in Web.xml, and when our class implements the Httpsessionbindlistener interface, the container will be divided into Do not automatically call the following two methods:

(1) void Valuebound (Httpsesssionbindevent event)

(2) void Valueunbound (Httpsessionbindingevent event)

Note that the trigger for this listener is for the class that implements the listener, and only if the class set that implements the listener is entered or removed from session, the listener is triggered

4, Httpsessionattributelistener

An operation that listens for attributes in a httpsession, triggers the attributeadded (httpsessionbindevent se) method when an attribute is added to the session, and when a property is deleted in the session, Triggers the attributeremoved (httpsessionbindevent se) method, triggering the attributereplaced (httpsessionbindingevent se) method when the session property is reset.

Note that the trigger for this listener is for all sessions, as long as the properties of the session change, this listener will be triggered

5, Httpsessionlistener

This has been written above, listening to httpsession. When a session is created, the sessioncreated (httpsessionevent se) method is triggered, and the sessiondestoryed (httpsessionevent se) method is triggered when a session is destroyed

6, Httpsessionactivationlistener

It's not too much, it's mostly listening to the same session moving to a different JVM

7, Servletrequestlistener and Servletrequestattributelistener

Similar to Servletcontextlistener and Servletcontextattributelistener, the former listens for the creation and destruction of request, the latter listens to the attributes of the request for additions and deletions.

Context-param

The key value pairs configured in Context-param are shared globally, and the entire Web project can be taken to this context, for example, I have an HTTP port and an HTTPS port configured in Web.xml:

<context-param>
    <param-name>NotSSLPort</param-name>
    <param-value>8080</ param-value>
</context-param>
<context-param>
    <param-name>sslport</ param-name>
    <param-value>8443</param-value>
</context-param>

The servlet can take this:

protected void HttpServletRequest (HttpServletResponse request, response thr) oWS servletexception, IOException {System.out.println ("Notsslport:" + getser 

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.