Java servlet (ii): servlet Configuration and Lifecycle correlation (jdk7+tomcat7+eclipse)

Source: Internet
Author: User

This article documents the servlet configuration-related usage and the servlet's life-cycle approach in the servlet container.

    • Tomcat is a servlet container:

1. The servlet container manages the entire life cycle of the servlet and invokes the servlet's life cycle method.

2. The servlet container is not just the life cycle of the servlet, but also the life cycle management container such as Jsp,filter,listener,tag.

    • Registration and operation of Servlets

1. The servlet program must start running through the servlet container, and the storage directory has special requirements, usually the servlet compiled. class files are stored in the <web application directory >\web-inf\classes\ directory.

2. The servlet program must register and map its access path in the Web application's. xml file before it can be loaded by the servlet engine for external access.

3, a <servlet> element user registers a servlet, which contains two primary child elements:<servlet-name> and <servlet-class> The user sets the registration name of the servlet and the full class name of the servlet, respectively.

4. An <servlet-mapping> element is used to map an external access path to a registered servlet, which contains two child elements:<servlet-name> and <url-pattern> The user specifies the registered name of the servlet and the external access path of the servlet, respectively.

    • Procedure for servlet container corresponding customer request:

1. The servlet engine checks to see if the instance object that was created for the servlet has been loaded. If it has already been created, perform step 4th directly, otherwise, step 2nd.

2. Mount and create an instance object of the servlet: Call the servlet's constructor.

3. Invoke the Init method of the Servlet instance object.

4. Create a ServletRequest object to encapsulate the request and a Servletresponse object that represents the corresponding message, then invoke the Servlet's service method and pass the request and the corresponding object as parameters.

5. Before the Web application is stopped or restarted, the servlet engine uninstalls the servlet and invokes the servlet's Destory method before uninstalling.

    • The Servlet life cycle method (the following methods are called by the servlet container):

1. The constructor is called first when the instance is created: only once, when the servlet is requested for the first time, an instance of the servlet is created, and the constructor is called.
2. Init method: Called only once, the Init function is called immediately after the servlet instance is created, and the user initializes the current servlet instance

3, service method: is called multiple times, each request will be called, for the corresponding request.

4. Destory method: Called only once, before the current servlet's web app is unloaded, to release the resources that the servlet occupies.

    • The servlet configuration requires considerations:

Load-on-startup configuration under the servlet node:

1, you can specify the time when the servlet was created;

2, the default parameter is negative, when the Web app is loaded, the servlet instance is not loaded, and is created on the first request;

3, if 0 or positive, the home instance is created when the current web app is loaded by the servlet, and the smaller the value, the earlier it is created.

Example configuration:

1 <!--Configuring and Mapping Servlets -2     <servlet>3         <!--Name of servlet registration -4         <Servlet-name>HelloServlet</Servlet-name>5         <!--servlet Full class name -6         <Servlet-class>Com.dx.hello.HelloServlet</Servlet-class>7         <Load-on-startup>1</Load-on-startup>8     </servlet>9     <servlet-mapping>Ten         <!--The registration name for the servlet-name under the servlet node is identical - One         <Servlet-name>HelloServlet</Servlet-name> A         <!--Map a specific access path, where/represents the root directory of the current web - -         <Url-pattern>/hello</Url-pattern> -     </servlet-mapping>
    • Details of the servlet mappings:

1. The same servlet can be mapped to multiple URLs, that is, the setting value of the <servlet-name> child elements of multiple <servlet-mapping> elements can be the registered name of the same servlet.

Like what:

1 <!--Configuring and Mapping Servlets -2     <servlet>3         <!--Name of servlet registration -4         <Servlet-name>HelloServlet</Servlet-name>5         <!--servlet Full class name -6         <Servlet-class>Com.dx.hello.HelloServlet</Servlet-class>7         <Load-on-startup>1</Load-on-startup>8     </servlet>9     <servlet-mapping>Ten         <!--The registration name for the servlet-name under the servlet node is identical - One         <Servlet-name>HelloServlet</Servlet-name> A         <!--Map a specific access path, where/represents the root directory of the current web - -         <Url-pattern>/hello</Url-pattern> -     </servlet-mapping> the     <servlet-mapping> -         <!--The registration name for the servlet-name under the servlet node is identical - -         <Servlet-name>HelloServlet</Servlet-name> -         <!--Map a specific access path, where/represents the root directory of the current web - +         <Url-pattern>/hello2</Url-pattern> -     </servlet-mapping>

This way you can access the same servlet through Http://localhost:8080/MyServlet001/hello and Http://localhost:8080/MyServlet001/hello2.

2. You can also use the * wildcard character in a servlet mapping to a URL, but there can be only two fixed formats: one is "*. Extension" and the other is to start with a forward slash "/" and End With "/".

Like what:

1 <servlet-mapping>2         <!--The registration name for the servlet-name under the servlet node is identical -3         <Servlet-name>HelloServlet2</Servlet-name>4         <!--Map a specific access path, where/represents the root directory of the current web -5         <Url-pattern>*.do</Url-pattern>6         <!--<url-pattern>*.html</url-pattern> -7     </servlet-mapping>8     <servlet-mapping>9         <!--The registration name for the servlet-name under the servlet node is identical -Ten         <Servlet-name>HelloServlet1</Servlet-name> One         <!--Map a specific access path, where/represents the root directory of the current web - A         <Url-pattern>/hello2/*</Url-pattern> -         <!--<url-pattern>/*</url-pattern> - -     </servlet-mapping>

But things to note:

1, *.html This write is no problem, if configured as/*.html or/*.action,/*.do and other formats, is the wrong configuration, loading will run out of abnormal information.

2, if the configuration is * No suffix name, you must be a light strap/, in the form of:/* or/a/* and so on.

Java servlet (ii): servlet Configuration and Lifecycle correlation (jdk7+tomcat7+eclipse)

Related Article

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.