Java学习

今天我在网上搜到了Servlet的生命周期

1.Servlet 遵循的过程:
  • Servlet 初始化后调用 init () 方法。
  • Servlet 调用 service() 方法来处理客户端的请求。
  • Servlet 销毁前调用 destroy() 方法。
  • 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。
2.init()方法的定义
public void init() throws ServletException { // 初始化代码... }
3.service() 方法
service() 方法是执行实际任务的主要方法。
service()方法的特征:
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{ }
service() 方法由容器调用,service 方法在适当的时候调用 doGet、doPost、doPut、doDelete 等方法。所以不用对 service() 方法做任何动作,只需要根据来自客户端的请求类型来重写 doGet() 或 doPost() 即可。
4.doGet()方法
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet 代码 }
5.doPost() 方法
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet 代码 }
6.destroy() 方法
destroy() 方法可以让您的 Servlet 关闭数据库连接、停止后台线程、把 Cookie 列表或点击计数器写入到磁盘,并执行其他类似的清理活动。
destroy 方法定义如下所示:
public void destroy() { // 终止化代码... }
遇到问题:没明白
明天继续学习
原文地址:https://www.cnblogs.com/wrljzb/p/14170715.html