Java Servlet series servlet entry

Source: Internet
Author: User

What is Servlet?

Servlet is a server-side Java application that has the characteristics of being independent from the platform and protocol and can generate dynamic web pages. It serves as the intermediate layer between customer requests (Web browsers or other HTTP client programs) and server responses (databases or applications on the HTTP server.

Servlet is a Java application on the server inside the Web server. Unlike the traditional Java application started from the command line, servlet is loaded by the Web server, the Web server must contain a Java virtual machine that supports servlet.

Servlet name

Web Server

A Web server is one or more machines on the Internet. System software and information that needs to be shared in the network are deployed on these machines. The client can access resources deployed on the server through HTTP.

Application Server

The application server, that is, the application server, is one or more machines on the network. System software is deployed on these machines to provide data services and business processing services for clients and other servers, such as web servers. Common application servers include file servers, printing servers, and EJB servers running EJB components.

Servlet Container

Servlet container, or servlet container, is a part of a Web server or application server. It can provide requests and responses to servlet services and manage the servlet lifecycle. Servlet containers can also provide other services for Servlets, such as resource services, security services, and thread services. Servlet containers can be integrated into Web servers or application servers, or embedded into Web servers or application servers as independent programs.

Servlet Functions

The servlet specification defines a programming framework to expand server functions, and uses the request-response mode to provide web-based services. When the client sends a request to the server, the server forwards the request information to the servlet. The servlet processes the request, generates the response content, and sends it to the web server, then the Web server returns the response to the client.

Servlet implementation process

The earliest Java Web server that supports Servlet technology is javasoft. Later, some other Java-based webservers began to support standard servletapis. The main function of servlet is to interactively browse and modify data to generate dynamic web content. This process is:

  1. The client sends a request to the server.
  2. The server sends the request information to the Servlet
  3. The servlet generates a response and sends it to the server. The dynamic generation of response content usually depends on the client request
  4. The server returns the response to the client.

The figure shows the interaction process between the client and the server servlet"
In fact, a servlet is a class in the Java programming language. It is used to expand the performance of the server. The server resides in an application that can be accessed through the "request-response" programming model. Although the servlet can respond to any type of requests, it is generally only used to expand the Web server applications.

Web Container

Web containers have two functions:

  1. Provides the APIS required for compiling servlet programs.
  2. Provides an environment for hosting and executing servlet programs
This servlet runtime environment can complete servlet program instantiation, initialization, calling service methods, and removal of servlet functions, that is, Web containers can manage the servlet lifecycle. Servlets reside in Web containers and are managed by web containers. The Web Container acts as a bridge between the Web server and Servlet by forwarding requests and responses.
Common Java Web containers include tomcat, JBoss, WebLogic, and websphere.
Correctly Understand That servletservlet cannot run the servlet independently and must run in the servlet container. The Container calls its lifecycle method. The preceding Web Container also has a declaration. Therefore, the servlet does not need the main method. Servlet is a common Java object. It implements the servlet interface and does not inherit from the thread. Therefore, servlet is not a thread. When the server receives a client request, it will execute the servlet method in a separate thread.
Comparison with traditional CGICompared with traditional CGI and many other CGI-like technologies, Java Servlet is more efficient, easier to use, more powerful, more portable, and more investment-saving. In the future technological development, Servlet may completely replace CGI.
In traditional CGI, each request starts a new process. If the CGI program runs for a short period of time, the overhead required to start the process may exceed the actual execution time. In servlet, each request is processed by a lightweight Java thread (rather than a heavyweight operating system process ).
In traditional CGI, if there are n concurrent requests to the same CGI program, the CGI program code is repeatedly loaded n times in the memory. For servlet, the request is processed by n threads and only one servlet code is required. In terms of performance optimization, Servlet also has more options than CGI. 1.
Servlet provides a large number of Utility Routines, such as automatically parsing and decoding HTML form data, reading and setting HTTP headers, processing cookies, and tracking session status.
2.
Powerful functions in servlet, many tasks that are difficult to accomplish using traditional CGI programs can be easily completed. For example, a servlet can directly interact with a Web server, but a common CGI program cannot. Servlet can also share data among various programs, making database connection pools and other functions easy to implement.
3.
Good portability servlet is written in Java, and servletapi has perfect standards. Therefore, servlets written for the iPlanet Enterprise Server can be transplanted to Apache, microsoftiis, or WebStar without any material changes. Almost all mainstream servers support servlets directly or through plug-ins.
4.
Cost saving: not only are there many low-cost or even free Web servers available for personal or small-scale websites, but also for existing servers that do not support Servlets, this feature is often free (or requires a very small investment ).

PS: Servlet is only the first article in the Java Servlet series. As the basic knowledge of Java Servlet, there will be several articles to continue to study servlet in depth. There are still some shortcomings in this article. You are welcome to criticize and correct them.

Reference: servlet details

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.