Javeweb Learning Servlet (i): Servlet life cycle and loading mechanism

Source: Internet
Author: User

Servlet life cycle and loading mechanism

To view the Servlet3.1 source code, its interface is defined as follows:

 PackageJavax.servlet;  Public InterfaceServlet { Public voidinit (servletconfig config)throwsservletexception;  Publicservletconfig getservletconfig ();  Public voidService (ServletRequest req, servletresponse Res)throwsservletexception, IOException;  PublicString getservletinfo ();  Public voiddestroy ();}
Init (servletconfig config);

The Init method is called by the container when the container is started and is only called once;
The timing of the call is related to the configuration item ' Load-on-startup ' of the servlet;

<servlet>    <Servlet-name>Springmvcdemo</Servlet-name>    <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>    <Init-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:springmvc.xml</Param-value>    </Init-param>    <Load-on-startup>1</Load-on-startup></servlet>

<load-on-startup>1</load-on-startup>: If the configured value is not less than 0, the identity container will load the servlet at startup, and the smaller the configured value, the higher the load priority If there is no configuration or the configured value is less than 0 o'clock, the container will be loaded when the servlet is first called, regardless of the configuration, the Init method will only be called once;

The parameters of the Init method are passed in by the container; The contextconfiglocation parameter that we configured in Web. XML is stored in the ServletConfig;

If we do not configure this parameter, it has the default value: Web-inf/${servletname}-servlet.xml, Variable ${servletname} is the ' Servlet-name ' configured in XML;

Getservletconfig ();

The Getservletconfig method is used to obtain servletconfig;

Service (ServletRequest req, servletresponse res);

The service method is used to process a request, and each access will be executed once, and the server method will invoke different doxxx methods depending on the request method;

Getservletinfo ();

The Getservletinfo method can obtain some servlet related information, such as author, copyright, etc.; This method needs to be implemented by itself, by default returning an empty string;

Destroy ();

The Destroy method is primarily used to release resources when the servlet is destroyed, and is only called once with Init;

Servlet Life Cycle Test Demo
  1. Create a new Web project, creating three Servletdemo objects that implement the Servlet interface;
    This is created by the servlet1,servlet2,servlet3, the code is the same as the code of the servlet 1 only paste;

     PackageCom.guitu18.servlet;Importjava.io.IOException;ImportJavax.servlet.Servlet;ImportJavax.servlet.ServletConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classServlet_1ImplementsServlet {@Override Public voiddestroy () {System.out.println ("Servlet_1:destroy () Execution destruction ..."); } @Override PublicServletConfig Getservletconfig () {return NULL; } @Override PublicString Getservletinfo () {return NULL; } @Override Public voidInit (ServletConfig arg0)throwsservletexception {System.out.println ("Servlet_1:init () performs initialization ..."); } @Override Public voidService (ServletRequest arg0, Servletresponse arg1)throwsservletexception, IOException {System.out.println ("Servlet_1:service () in execution ..."); }}
  2. The Web. XML is configured as follows:

    <servlet>    <Servlet-name>Servlet_1</Servlet-name>    <Servlet-class>Com.guitu18.servlet.Servlet_1</Servlet-class></servlet><servlet-mapping>    <Servlet-name>Servlet_1</Servlet-name>    <Url-pattern>/servlet_1</Url-pattern></servlet-mapping><servlet>    <Servlet-name>Servlet_2</Servlet-name>    <Servlet-class>Com.guitu18.servlet.Servlet_2</Servlet-class>    <Load-on-startup>2</Load-on-startup></servlet><servlet-mapping>    <Servlet-name>Servlet_2</Servlet-name>    <Url-pattern>*.guitu18</Url-pattern></servlet-mapping><servlet>    <Servlet-name>Servlet_3</Servlet-name>    <Servlet-class>Com.guitu18.servlet.Servlet_3</Servlet-class>    <Load-on-startup>1</Load-on-startup></servlet><servlet-mapping>    <Servlet-name>Servlet_3</Servlet-name>    <Url-pattern>/abc/*</Url-pattern></servlet-mapping><servlet-mapping>    <Servlet-name>Servlet_2</Servlet-name>    <Url-pattern>/servlet_4</Url-pattern></servlet-mapping>

    I configured these three servlets in XML separately, CONFIGURED 4 servlet-mapping, where '/servlet2.guitu ' and '/servlet4 ' are all pointing to servle_2;
    This involves the configuration of <url-pattern> in the servlet, a total of 3 kinds:

      1. Full path matching
        Start with a '/'; for example:/servlet_1
      2. Catalog Matching
        Start with '/' and End With '/'; For example:/abc/
      3. Name extension matches
        Do not start with '/', forexample:. guitu18

    In order to be non-ambiguous when matching, these three matching methods are prioritized:
    Full path matching > directory matching > Extension matching

  3. Startup Project:
    Because both servlet2 and servlet3 are configured with <load-on-startup>, and the configured value is not less than 0, servlet2 and servlet3 are initialized when the project starts. Console output:

    Servlet_3:init()执行初始化...  Servlet_2:init()执行初始化...  

    As seen here, Servlet 3 is limited to initializationthan servlet2 because the value of the <load-on-startup> of the servlet_3 configuration is smaller than the servlet2, so precedence is loaded; Servlet 1 because there is no configuration <load-on-startup> so only the first time it is accessed will be loaded initialization;

    address for access to servlet 1: Http://localhost:8080/servletdemo/servlet1

    Servlet_1:init()执行初始化...Servlet_1:service()执行中...

    At this time servlet_1 is initialized;

  4. Servlet object Destruction Now close the Tomcat service and the Servlet object will be destroyed;

    Servlet_2:destroy()执行销毁...Servlet_3:destroy()执行销毁...Servlet_1:destroy()执行销毁...

    It can be seen that the order of destruction is exactly the same as the order of loading, and priority loading will be destroyed first;

Javeweb Learning Servlet (i): Servlet life cycle and loading mechanism

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.