Java Web笔记 - Servlet技术介绍 生命周期 核心API 类方法调用顺序

发布于 2011-11-10 | 更新于 2020-09-20

1、Servlet技术:

Servlet是和平台无关的服务器端组件,它运行再Servlet容器中。Servlet容器负责Servlet和客户端通信和调用Servlet方式。Servlet和客户端通过“请求/响应”模式。

2、Servlet的功能:

创建并返回基于客户请求的HTML页面 创建可嵌入现有HTML页面中的部分HTML代码片段 与其他服务器资源通信

3、Servlet框架涉及到的两个JAR包:

javax.servlet包:定义了所有的Servlet类都必须实现或扩展的通用接口和类。 javax.servlet.http包:定义了采用HTTP协议通信的HttpServlet类。

4、Servlet的相关类和Servlet的生命周期:

Servlet框架的核心是Javax.servlet.Servlet接口,所有的Servlet都必须实现这一个接口中定义的五个方法。

4.1、其中的三个方法代表了Servlet的生命周期:

init方法:初始化Servlet service方法:响应客户请求 destroy方法:Servlet对象退出生命周期时,释放占用的资源

4.1.1、Servlet容器创建Servlet的时机:

① Servlet容器启动时自动装载某些Servlet

默认情况下,当Web客户第一次请求访问某个Servlet时,才创建这个Servlet的实例。如果设置了元素的子元素,Servlet容器在启动Web应用时,将按照指定的顺序创建并初始化这个Servlet:

LoginServlet com.itzhai.login.LoginServlet 2

可以在这种servlet中编写全局的初始化信息。

② Servlet容器启动后,客户首次向Servlet发送请求

③ Servlet类文件被更新后,重新装载Servlet

Servlet被装载后,Servlet容器创建一个Servlet实例并调用Servlet的init()方法进行初始化。在Servlet整个生命周期中,init方法只被调用一次。

4.1.2、Servlet的响应请求阶段:

对于到达Servlet容器的客户请求,Servlet容器创建特定于这个请求的ServletRequest和ServletResponse对象,然后调用service方法进行处理。service方法从ServletRequest对象中获取客户请求信息,处理请求,并通过ServletResponse对象返回根客户端响应结果。

4.1.3、Servlet的终止阶段:

当Web应用被终止或者Servlet容器结束运行时,或Servlet容器重新装载Servlet的新实例时,Servlet容器会先调用Servlet的destroy方法释放Servlet所占用的资源。

5、ServletRequest和ServletResponse接口

5.1、ServletRequest接口:

ServletRequest接口中封装了客户端请求信息,客户端正在使用的协议,还提供了直接以二进制读取客户端数据流的ServletInputStream。

ServletRequest子类提供了更多的和特点协议相关的数据,如HttpServletRequest。

ServletRequest接口的主要方法:

getAttribute getContentType getInputStream getParameter getRemoteAddr getRemoteHost getRemotePort

5.2、ServletResponse接口:

ServletResponse接口为Servlet提供了返回相应结果的方法。允许Servlet设置返回数据的长度和MIME类型,并提供输出流ServletOutputStream。

ServletResponse子类提供了更多的和特点协议相关的数据,如HttpServletResponse。

ServletResponse接口的主要方法:

getOutputStream getWriter getCharacterEncoding getContentType setContentType

6、Servlet核心API及其的继承关系图:

6.1、Servlet类:

public interface Servlet
Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

1. The servlet is constructed, then initialized with the init method.
2. Any calls from clients to the service method are handled.
3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

Servlet的相关方法:

service
void service(ServletRequest req,
ServletResponse res)
throws ServletException,
IOException

Called by the servlet container to allow the servlet to respond to a request.

This method is only called after the servlet’s init() method has completed successfully.

6.2、通用的Servlet GenericServlet类:

public abstract class GenericServlet
extends Object
implements Servlet, ServletConfig, Serializable

Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead.

GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a servlet, although it’s more common to extend a protocol-specific subclass such as HttpServlet.

6.3、HttpServlet类:

public abstract class HttpServlet
extends GenericServlet
implements Serializable

Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:

• doGet, if the servlet supports HTTP GET requests
• doPost, for HTTP POST requests
• doPut, for HTTP PUT requests
• doDelete, for HTTP DELETE requests
• init and destroy, to manage resources that are held for the life of the servlet
• getServletInfo, which the servlet uses to provide information about itself

There’s almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).

Likewise, there’s almost no reason to override the doOptions and doTrace methods.

6.4、HttpServlet类中各个方法的调用顺序:

首先调用Service方法,再根据请求信息,调用具体的getGet或doPost等方法。

由于Servlet是运行在多线程的服务器,所有需要特别处理并发共享的资源。

每个Servlet都需要实现Servlet接口。GenericServlet是一个通用的,不特定于协议的Servlet,其实现了Servlet接口,而HttpServlet继承与GenericServlet,实现了域HTTP协议相关的操作,所以我们定义的Servlet只需要直接继承HttpServlet即可。

本文作者: arthinking

本文链接: https://www.itzhai.comjava-web-notes-servlet-life-cycle-of-the-core-api-technology-introduced-class-method-call-sequence.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。