What is Servlet?

Source: Internet
Author: User

Servlet is a Java program that uses Java Servlet application design APIs and related classes and methods. In addition to the Java Servlet API, Servlet can also use Java software packages used to expand and add to the API. Servlet runs on a Java-enabled Web server or application server and extends the capabilities of this server. Java servlet is like a Java applet to a Web browser for a Web server. Servlet is loaded into the Web server and executed in the Web server, while applet is loaded into the Web browser and executed in the Web browser. The Java Servlet API defines a standard interface between a servlet and a Java-enabled server, which makes Servlets a cross-server platform feature.
By creating a framework, Servlet extends the server's capabilities to provide Web-based request and response services. When the client sends a request to the server, the server can send the request information to the Servlet, and let the Servlet establish a response from the server to the client. The Servlet can be automatically loaded when the Web server or client requests the service for the first time. After loading, the Servlet continues to run until other clients send a request. Servlet has a wide range of functions. For example, Servlet can complete the following functions:

(1) Create and return a complete HTML page containing dynamic content based on the nature of customer requests.

(2) create an HTML page (HTML segment) that can be embedded in an existing HTML page ).

(3) communicate with other server resources (including databases and Java-based applications.

(4) process connections with multiple clients, receive input from multiple clients, and broadcast the results to multiple clients. For example, Servlet can
It is a game server with multiple participants.

(5) When data transmission is allowed in a single connection mode, open a new connection from the server to the applet in the browser and connect the connection
The connection is enabled. When the client and server are allowed to execute sessions in a simple and efficient manner, the applet can also start the connection between the client browser and the server. Communication can be performed through custom protocols or standards (such as IIOP.

(6) use MIME type to filter data for special processing, such as image conversion and server-side (SSI ).

(7) provide customized processing to standard routines for all servers. For example, Servlet can modify how to authenticate users.

2. Servlet lifecycle:

The Servlet lifecycle begins when it is loaded into the memory of the Web server and ends when it is terminated or re-loaded.

(1) initialization
Load the Servlet at the following time:
= If the automatic loading option is configured, it is automatically loaded when the server is started.
= When the client sends a request to the Servlet for the first time after the server is started
= When the Servlet is reloaded
After the Servlet is loaded, the server creates a Servlet instance and calls the Servlet init () method. During the initialization phase, the Servlet initialization parameters are passed to the Servlet configuration object.

(2) Request Processing
For client requests sent to the server, the server creates a "request" object and a "response" Object specific to the request. The server calls the Servlet service () method, which is used to pass the "request" and "response" objects. The service () method obtains the request information from the "request" object, processes the request, and uses the "response" Object method to send the response back to the client. The service () method can call other methods to process requests, such as doGet (), doPost (), or other methods.

(3) Termination
When the server no longer needs a Servlet or reloads a new Servlet instance, the server will call the Servlet's destroy () method.


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.