A servlet can create protocols, platform-independent web applications, applets run in the browser's JRE, and the servlet runs in the servlet container of the Web application server, and the servlet does not have a user graphical interface. Servlet and the Web application server's servlet container interact to receive requests to return responses. The request was first processed by the Web application server's servlet container and passed to Servlet,servlet to return the response to the client via the Web application server. A client program can use any language development that can send a request to a Web application server.
The biggest advantage of the servlet is its high performance, the servlet uses a very different way of running with CGI, first the servlet loads and resides in memory at the first initialization, and then runs directly from memory, and second, the servlet works in single instance multithreading by default , when a new request arrives, the servlet instance opens the request for a new thread service.
Servlet Architecture and Thread safety
All servlet implementations Javax.servlet.Servlet interfaces directly or indirectly, which specify how the servlet communicates with the servlet container, and also define the lifecycle of the servlet. Genericservlet is a protocol-independent universal Servlet,httpservlet that is a servlet,web application developed specifically for HTTP protocols that directly inherits HttpServlet. The inheritance system of its class is shown in the following illustration:
Figure 1 The class inheritance system of the servlet
The Javax.servlet.Servlet interface includes 3 ways to control the servlet lifecycle, respectively:
init (servletconfig config) method
When the servlet initializes, the init () method is called to perform the work of initializing the servlet, and the Init () method is invoked only once. When the servlet is initialized, it enters a ready state, ready to respond to client requests.
Service (ServletRequest req, Servletresponse resp) method
The servlet container invokes the service () method to process the request and return the response. ServletRequest and Servletresponse are passed as incoming parameters to the service (), ServletRequest encapsulates the requested information and Servletresponse encapsulates the response information.
Destroy () method
The servlet container can uninstall the servlet at any time, at which point Destory () is invoked, where you can release the resources that the servlet occupies.
The classes in the Javax.servlet.http package are used to support HTTP protocols and create HTML Web pages. The HTTP protocol is based on the request/response mode of operation, and these HTTP requests include:
Get
POST
Put
DELETE
Head
TRACE
CONNECT
OPTIONS