Interpretation Servlet of the J2EE 13 specification and j2ee13 interpretation servlet

Source: Internet
Author: User

Interpretation Servlet of the J2EE 13 specification and j2ee13 interpretation servlet

 

  What is Servlet?

 

First, Servlet is a specification developed by Sun for server-side components.

Second, the Servlet can run on the web server or application server.

Third, servlets are managed by Servlet containers, such as "instantiate and manage Servlet objects" and "Call Servlet lifecycles ".

To parse and encapsulate requests and responses to specific protocols.

Fourth, as the middle layer between requests from Web browsers and databases or applications, Servlet can collect user requests from Web pages.

And displays records from databases or other sources. This allows you to dynamically create webpages.

 

  

 

Servlet Architecture

 

Shows the Servlet position in the Web application.

  Servlet Lifecycle

 

The Servlet lifecycle can be defined as the entire process from creation to destruction. When a user calls a Servlet, a Servlet is created.

Instance, each user request generates a new thread.

The above is the process followed by the Servlet lifecycle.

  • Initialize by calling the init () method.
  • Call the service () method to process client requests.
  • Terminate (end) by calling the destroy () method ).
  • Finally, the Servlet is garbage collection by the JVM garbage collector.

Now let's discuss in detail the lifecycle method.

 

  Explanation of Servlet Method

 

  Init () method

The init method is designed to be called only once. It is called when a Servlet is created for the first time, and is no longer called at each subsequent user request. Therefore, it

Is used for one-time initialization. The init () method simply creates or loads some data, which will be used throughout the Servlet lifecycle.

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 main method for executing the actual task. The Servlet container calls the service () method to process

And write the formatted response back to the client.

Each time the server receives a Servlet Request, the server generates a new thread and calls the service. Service () method check HTTP

Request type (GET, POST, PUT, DELETE, etc.), and call doGet, doPost, doPut, doDelete, and other methods as 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. When appropriate, the service method calls doGet, doPost, doPut, doDelete, and other methods.

Therefore, we do not need to perform any action on the service () method. You only need to reload doGet () or

DoPost.

 

  

  DoGet () method

A GET request comes from a normal request of a URL or an HTML form with no submission method specified. It is processed 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>


 

  DoPost () method

The post request comes from an HTML form that specifically specifies the submission method as POST, which is processed 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 at the end of the Servlet lifecycle. The destroy () method enables Servlet

Close the database connection, stop background threads, write the Cookie list or click the counter to the disk, and execute other similar cleanup activities.

After the destroy () method is called, The servlet object is marked as garbage collection. The destroy method is defined as follows:

  

<Span style = "font-family: SimSun;"> public void destroy () {// termination code... } </Span>

  

 

 

 

The first time you contact Servlet, you can understand it as Handler in. NET, because they have a similar role: can be used to handle requests sent by the client.

Allows the front-end and business logic to be loosely coupled and allows the creation of dynamic web pages.

Due to lack of cognition, we can only say this first. If this is the case, you need to have a deep understanding of the actual application before you can make a decision. Learn New now

I hope to find some corresponding shadows from the previously learned knowledge. Although they are not so corresponding, It may be wrong to understand this,

With the continuous in-depth study, we will naturally correct these deviations, leaving us more impressed.

 

 

For your reference only. Do not mislead anyone.

 

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.