HttpServlet类介绍

HttpServlet类扩展GenericServlet类并实现Serializable接口。
它提供特定于http的方法,例如doGet,doPost,doHead,doTrace等。(Serializable接口是干嘛的?

Servlet小服务程序的生命周期

Web容器维护Servlet实例的生命周期

Servlet小服务程序可以粗略的看作有三种状态

  1. 准备就绪
  2. 执行任务
  3. 结束状态

HttpServlet类与Servlet的生命周期

As displayed in the above diagram, there are three states of a servlet: new, ready and end. The servlet is in new state if servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.
如上图所示,小服务程序有三种状态:新建,就绪和结束。 如果创建了servlet实例,则servlet处于新状态。 调用init()方法后,Servlet进入就绪状态。 在就绪状态下,servlet执行所有任务。 当Web容器调用destroy()方法时,它将转换为结束状态。

Servlet的创建一共有五个步骤

java对象的创建经历三个步骤
- 类加载
- 实例化
- 初始化
同样的Servlet实例的创建也经历这三个步骤(具体这三个步骤有什么区别可以参考面向对象中的对象Object的创建那一章节的内容)
最后的两个方法是服务方法和销毁方法。
服务方法
主要方法,是响应和发送请求的方法
销毁方法
Web容器在从服务中删除Servlet实例之前调用destroy方法。 它为Servlet提供了清理任何资源(例如内存,线程等)的机会

相关文章: