zoukankan      html  css  js  c++  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() { // 终止化代码... }

  • 相关阅读:
    整数反转
    两数之和
    设计模式-备忘录模式
    设计模式-迭代器模式
    设计模式-中介者模式
    设计模式-观察者模式
    C# OpenFileDialog和SaveFileDialog的常见用法
    SQL数据库表结构的修改(sql2005)
    C# 时间格式处理
    C# 集合类(四)
  • 原文地址:https://www.cnblogs.com/zyj3955/p/14081502.html
Copyright © 2011-2022 走看看