Java servlet lifecycle

Source: Internet
Author: User
Tags garbage collection http post

A servlet is a Java program that uses Java servlet applications to design interfaces and related classes and methods. It runs on the Web server or on the application server and expands the capabilities of the server. The Java servlet is like a Java applet for a Web browser. Applets are mounted in a Web browser and executed in a Web browser, while the servlet is mounted on the Web server and executed within the Web server. The Java servlet API defines a standard interface between the servlet and the server, which enables the servlet to have the characteristics of a cross server platform.

The servlet provides Web services in request-response mode by creating a framework to extend the server's capabilities. When the client sends a request to the server, the server sends the request information to Servlet,servlet to generate the response content and passes it to the server, which then returns the response to the client by the server.

The functionality of the servlet

The functionality of the servlet covers a wide range of functions. For example, the servlet can complete the following functions:

Creates and returns a complete HTML page that contains dynamic content based on the nature of the customer request.

Create a portion of an HTML page (an HTML fragment) that can be embedded in an existing HTML page. Communicates with other server resources (files, databases, applets, Java applications, and so on).

Handles connections with multiple clients, accepts input from multiple clients, and broadcasts the results to multiple clients. For example, a Servlet can be a game server for multiple participants.

Allows you to open a new connection to the applet on the browser and keep the connection open by allowing the data to be transferred in a single connection. The applet can also initiate a connection between the client browser and the server, allowing the client and the server to perform the session simply and efficiently. You can communicate by customizing protocols or standards, such as IIOP.

A MIME type is used to filter data for special processing, such as image conversion and server-side (including SSI).

A standard routine that delivers customized processing to all servers. For example, the Servlet can modify how users are authenticated.

Differences between Java servlet and existing network technology

So what is the specific difference between the Java servlet and some of the existing network technologies?

The difference between the Java servlet and the applet basically, the servlet is diametrically opposed to the applet. A servlet can be considered a server-side Applet. The servlet runs within a network server and the applet runs within a Web browser. The browser submits a command to execute the servlet, and because the browser can command the applet directly, it can run on a stand-alone computer at run time.

The difference between a Java servlet and a CGI is more efficient, easier to use, more powerful, more portable, and less expensive than traditional CGI and many other CGI-like technologies. One of the biggest differences is in performance.

In traditional CGI, each request starts a new process, and if the CGI program itself is executing for a short time, the overhead of starting the process is likely to exceed the actual execution time. When using a servlet, only one Java virtual machine is running on the server, and it is loaded only when the servlet is invoked, and it is not loaded again until the servlet changes. In traditional CGI, if there are n concurrent requests for the same CGI program, the CGI program's code is repeatedly loaded n times in memory, and for the servlet, the request is n threads, and only one servlet class code is required. In terms of performance tuning, the servlet has more options than CGI, such as buffering previous calculations, keeping database connections active, and so on.

By using the servlet API, developers do not have to worry about how the server works internally. form information, server headers, cookies can be processed through the servlet. Also, because the servlet is written in Java, it can be moved from one server to another for publishing without worrying about the type of operating system or server. This advantage fully embodies the Java "once written, run anywhere" superior characteristics.

The life cycle of the servlet

The lifecycle of the servlet defines how a servlet is loaded, initialized, and how it receives requests, responds to requests, and provides services.

In code, the servlet lifecycle is defined by the interface Javax.servlet.Servlet. All Java Servlet must implement the Javax.servlet.Servlet interface directly or indirectly in order to run on the servlet engine. Servlet engine provides network Service, responds to MIME request, and runs Servlet Container. The Javax.servlet.Servlet interface defines methods that, in the lifecycle of the servlet, are invoked in a certain order at certain times. As shown in Figure 14-1.

  
The life cycle of the servlet

How the Servlet is loaded (load), instantiated (instantiated)

The servlet Engine is responsible for instantiating and loading the servlet, which can be executed when the servlet Engine load, either when the servlet responds to the request, or at any time between the two.

How the servlet is initialized (initialized)

After the servlet Engine loads the servlet, it must be initialized. During initialization, the servlet can read the initial data from the database, establish a JDBC Connection, or establish a reference to other valuable resources.

In the initialization phase, the Init () method is invoked. This method is defined in the Javax.servlet.Serlet interface. The Init () method takes a servlet configuration file (ServletConfig type) as a parameter. The Servlet configuration object is implemented by the servlet Engine, which allows the servlet to read some of the parameter values of the Name-value pairs. The ServletConfig object also allows the servlet to accept a servlet context object.

How the Servlet handles requests

After the Servlet is initialized, it is in a ready state to respond to the request. Each request to the servlet is represented by a servlet request object. The servlet's response to the client is represented by a servlet response object. When a client has a request, the servlet Engine the ServletRequest and Servletresponse objects to the servlet, which is passed to the service method as parameters. This method is defined by Javax.servlet.Servlet and implemented by a specific servlet.

The servlet can also implement ServletRequest and Servletresponse interfaces. The ServletRequest interface allows the servlet to obtain parameters from the client request, such as form data, request information, protocol type, and so on. The Servlet can read the request data from the ServletInputStream stream. The Servletresponse interface allows the servlet to set response headers and status codes. Implementing this interface enables the servlet to access the Servletoutputstream stream to return data to the client.

How the servlet is freed

Servlet Engine It is not necessary to maintain the status of the servlet for every period of time in the servlet lifecycle. Servlet engine can freely use or release the servlet at any time. Therefore, you cannot rely on the servlet class or its members to store information. When the servlet engine determines that a servlet should be freed (for example, engine is ready to shut down or needs to reclaim resources), engine must allow the servlet to release any resources it is using and preserve persistent state information.     These can be implemented by invoking the Destroy method of the servlet. Before servlet Engine releases a servlet, it must either complete the service method of the current instance or wait until timeout (if Engine defines timeout). When engine releases a servlet, engine will no longer be able to forward requests to it, engine must completely release the servlet and mark it as recyclable (to garbage collection).

The most important thing in the Servlet API is the servlet interface. All servlets perform this interface in a variety of ways: either directly, or by extending this class to execute it, such as HttpServlet. This servlet interface provides and arranges a way for the servlet to contact the client. Servlet writers can provide more or all of these methods when they develop a servlet program.

When a servlet receives a call request from the client, it receives two objects: one is ServletRequest, the other is servletresponse. This ServletRequest class outlines the connection from the client to the server, while the Servletresponse class outlines the connection from the servlet back to the client.

ServletRequest interface can obtain such information as the parameter name that is passed by the client, the protocol that the client is using, and the remote host name of the server that generated the request and received the request. It also provides a Servlet, servletinputstream that gets the data stream, which is submitted using the HTTP post and put method in the client reference. A servletrequest subclass allows the servlet to obtain more protocol attribute data.

For example, HttpServletRequest contains methods for obtaining Http-specific header information. Servletresponse interface gives the corresponding servlet method to the client. It allows the servlet to set the content length and the MIME type of the response, and provides an output stream servletoutputstream, through which the author can send back the corresponding data. Servletresponse subclasses can give you more information about the protocol-specific capacity. For example, HttpServletResponse contains methods that allow the servlet to manipulate http-specific header information.

The above description of classes and interfaces constitutes a basic servlet framework. HTTP Servlets has some additional methods that can provide session-tracking capabilities. Servlet writers can use these APIs to maintain the state between the servlet and the client when they have other actions.

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.