zoukankan      html  css  js  c++  java
  • servlet 之 servlet接口详解

    1. package javax.servlet;   //Tomcat源码版本:6.0.20    
    2.   import java.io.IOException;    
    3.       
    4.   public interface Servlet {       
    5.   
    6.   //负责初始化Servlet对象。容器一旦创建好Servlet对象后,就调用此方法来初始化Servlet对象      
    7.      public void init(ServletConfig config) throws ServletException;        
    8.   
    9.    //方法负责响应客户的请求,提供服务。当容器接收到客户端要求访问特定的servlet请求时,就会调用Servlet的service方法      
    10.       public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;        
    11.     /*   
    12.       *Destroy()方法负责释放Servlet 对象占用的资源,当servlet对象结束生命周期时,servlet容器调用此方法来销毁servlet对象.   
    13.     **/    
    14.       public void destroy();    
    15.       /*   
    16.       说明:Init(),service(),destroy() 这三个方法是Servlet生命周期中的最重要的三个方法。   
    17.       **/       
    18.       /*   
    19.       *返回一个字符串,在该字符串中包含servlet的创建者,版本和版权等信息   
    20.       **/    
    21.       public String getServletInfo();      
    22.           
    23.       /*   
    24.      GetServletConfig: 返回一个ServletConfig对象,该对象中包含了Servlet初始化参数信息    
    25.      **/        
    26.       public ServletConfig getServletConfig();      
    27.   } 
    28.   

    init方法:

    init(ServletConfig config)

      Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

    在 Servlet 的生命期中,仅执行一次 init() 方法,它是在服务器装入 Servlet 时执行的。

    可以配置服务器,以在启动服务器或客户机首次访问 Servlet 时装入 Servlet。 无论有多少客户机访问 Servlet,都不会重复执行 init() 。 

    缺省的 init() 方法通常是符合要求的,但也可以用定制 init() 方法来覆盖它,典型的是管理服务器端资源。 例如,可能编写一个定制 init() 来只用于一次装入 GIF 图像,改进 Servlet 返回 GIF 图像和含有多个客户机请求的性能。另一个示例是初始化数据库连接。缺省的 init() 方法设置了 Servlet 的初始化参数,并用它的 ServletConfig 对象参数来启动配置, 因此所有覆盖 init() 方法的 Servlet 应调用 super.init() 以确保仍然执行这些任务。在调用 service() 方法之前,应确保已完成了 init() 方法。 

     

    service方法:

    service(ServletRequest req, ServletResponse res)

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

    service() 方法是 Servlet 的核心。每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要被调用,而且传递给这个方法一个"请求"(ServletRequest)对象和一个"响应"(ServletResponse)对象作为参数。 在 HttpServlet 中已存在 service() 方法。缺省的服务功能是调用与 HTTP 请求的方法相应的 do 功能。例如, 如果 HTTP 请求方法为 GET,则缺省情况下就调用 doGet() 。

    Servlet 应该为 Servlet 支持的 HTTP 方法覆盖 do 功能。因为 HttpServlet.service() 方法会检查请求方法是否调用了适当的处理方法,不必要覆盖 service() 方法,只需覆盖相应的 do 方法就可以了。 


    post方法与get方法:
    当一个客户通过HTML 表单发出一个HTTP POST请求时,doPost()方法被调用。与POST请求相关的参数作为一个单独的HTTP 请求从浏览器发送到服务器。当需要修改服务器端的数据时,应该使用doPost()方法。 
    当一个客户通过HTML 表单发出一个HTTP GET请求或直接请求一个URL时,doGet()方法被调用。与GET请求相关的参数添加到URL的后面,并与这个请求一起发送。当不会修改服务器端的数据时,应该使用doGet()方法。 

    一般开发采用post方法,大小一般限制为64KB。

     

    destroy方法:

    destroy()
              Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. 

    与init方法相类似,destroy() 方法仅执行一次,即在服务器停止且卸装Servlet 时执行该方法。典型的,将 Servlet 作为服务器进程的一部分来关闭。缺省的 destroy() 方法通常是符合要求的,但也可以覆盖它,典型的是管理服务器端资源。例如,如果 Servlet 在运行时会累计统计数据,则可以编写一个 destroy() 方法,该方法用于在未装入 Servlet 时将统计数字保存在文件中。另一个示例是关闭数据库连接。 
    当服务器卸装 Servlet 时,将在所有 service() 方法调用完成后,或在指定的时间间隔过后调用 destroy() 方法。一个Servlet 在运行service() 方法时可能会产生其它的线程,因此请确认在调用 destroy() 方法时,这些线程已终止或完成。 

     

    getServletInfo方法:

    getServletInfo()
              Returns information about the servlet, such as author, version, and copyright. 

    GetServletInfo()方法是一个可选的方法,它提供有关servlet 的信息,如作者、版本、版权。 
    当服务器调用sevlet 的Service()、doGet()和doPost()这三个方法时,均需要 "请求"和"响应"对象作为参数。"请求"对象提供有关请求的信息,而"响应"对象提供了一个将响应信息返回给浏览器的一个通信途径。 

     

    getServletConfig方法:

    getServletConfig()
              Returns a ServletConfig object, which contains initialization and startup parameters for this servlet. 

    getServletConfig() 方法返回一个 ServletConfig 对象,该对象用来返回初始化参数和 ServletContext。ServletContext 接口提供有关 servlet 的环境信息。

  • 相关阅读:
    Android Market google play store帐号注册方法流程 及发布应用注意事项【转载】
    cocos2d-x 调用第三方so文件
    ios cocos2d-x 多点触摸
    linux文件分割(将大的日志文件分割成小的)
    Linux 统计某个字符串出现的次数
    scapy基础-网络数据包结构
    mac 下idea光标问题
    mac ox终端显示 bogon的问题
    hibernate和mybatis的区别
    memcached知识点梳理
  • 原文地址:https://www.cnblogs.com/shenhaha520/p/8568550.html
Copyright © 2011-2022 走看看