Interpretation of the 13 specification of the Java EE servlet

Source: Internet
Author: User

  What is a Servlet?

First, Servlets are a specification of server-side components that are developed by Sun Company.

Second, the Servlet can run on a Web server or an application server.

Third, the servlet is managed by a servlet container that implements such things as "instantiating and managing a Servlet object", "invoking the servlet life cycle Party

Resolution and encapsulation of requests and responses for specific protocols.

IV, as a request from a Web browser and a middle tier between a database or application, a servlet can collect users from a Web page,

Data from a database or other source, enabling the dynamic creation of Web pages.

  

Servlet schema diagram

Shows the location of the Servlet in the Web application.

  Servlet life cycle

The Servlet life cycle can be defined as the entire process from creation to destruction. When a user invokes a servlet, a servlet is created

instance, each user request will result in a new thread.

These are the processes that the Servlet life cycle follows.

    • Initialized by calling the Init () method.
    • Call the service () method to process the client's request.
    • Terminates (ends) by calling the Destroy () method.
    • Finally, the Servlet is garbage collected by the JVM's garbage collector.

Now let's discuss the life cycle approach in detail.

  Interpretation of the Servlet method

  Init () method

The Init method is designed to be called only once. It is called the first time the Servlet is created, and is no longer invoked on subsequent user requests. Therefore, it

is used for one-time initialization, the init () method simply creates or loads some data that will be used for the entire life cycle of the Servlet.

The Init method is defined as follows:

  

<span style= "Font-family:simsun;" >public void Init () throws Servletexception {//initialization code ...} </span>

Service () method

The service () method is the primary way to perform the actual task. The Servlet container invokes the service () method to process from the client (browse

device) request, and writes the formatted response back to the client.

Each time the server receives a Servlet request, the server generates a new thread and invokes the service. The service () method checks the HTTP

Request type (GET, POST, PUT, DELETE, etc.), and call Doget, DoPost, Doput,dodelete and so on when appropriate.

Method definition:

<span style= "Font-family:simsun;" ><span style= "FONT-SIZE:18PX;" >public void Service (ServletRequest request,servletresponse response)       throws Servletexception, ioexception{} </span></span>

The service () method is called by the container, and the service method calls Doget, DoPost, DoPut, DoDelete, and so on when appropriate.

Therefore, we do not need to do any action on the service () method, you only need to overload doget () or by the request type from the client.

DoPost ().

  

  Doget () method

A GET request comes from a normal request for a URL, or an HTML form that does not specify a submission method, which is handled by the Doget () method.

  

<span style= "Font-family:simsun;" ><span style= "FONT-SIZE:18PX;" >public void doget (HttpServletRequest request, httpservletresponse response)    throws Servletexception, IOException {    //Servlet code}</span></span>


  DoPost () method

The POST request comes from a specially specified HTML form that is submitted as a POST, which is handled by the DoPost () method.

  

<span style= "Font-family:simsun;"  >public void DoPost (httpservletrequest request,httpservletresponse response) throws Servletexception, IOException { Servlet Code}</span>

Destroy () method

The Destroy () method is called only once and is called at the end of the Servlet's life cycle. The Destroy () method enables the Servlet

Close the database connection, stop the background thread, write the Cookie list or hit counter to disk, and perform other similar cleanup activities.

After the Destroy () method is called, the Servlet object is marked as garbage collection. The Destroy method definition is as follows:

  

<span style= "Font-family:simsun;" >public void Destroy () {//Terminator code ...} </span>

  

The first contact with the servlet can be understood as handler in. NET because they have a similar effect: they can be used to handle the requests sent by the client.

To allow the foreground to be loosely coupled with the business logic and to create dynamic Web pages.

Because of the lack of cognition, can only say that understanding first.  In particular, this also requires practical application in-depth understanding before making judgments. Now learn new

Knowledge, it is hoped to find some of the original knowledge of the corresponding shadow, although not so corresponding, it is possible to understand this is wrong, but

As we continue to learn in depth, we will naturally revise these deviations and leave a deeper impression.

For reference only, hope not to mislead everyone.

Interpretation of the 13 specification of the Java EE servlet

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.