zoukankan      html  css  js  c++  java
  • Servlet技术——常用的类和接口

    Servlet是运行在服务器端的Java应用程序,由Servlet容器对其进行管理。

    当用户对容器发送HTTP请求时,容器将通知相应的Servlet对象进行处理,完成用户和程序之间的交互。

    在Servlet编程中,Servlet API提供了标准的接口和类。这些对象对Servlet的操作非常重要,它们为HTTP请求和程序回应提供了丰富的方法。

    1、Servlet接口
    在Servlet开发中,任何一个Servlet对象都要直接或间接地实现javax.servlet.Servlet接口。

    下面是Java EE API中对该接口的描述:

    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: The servlet is constructed, then initialized with the init method. Any calls from clients to the service method are handled. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized. In addition to the life-cycle methods, this interface provides the getServletConfig method,
    which the servlet can use to get any startup information, and the getServletInfo method,
    which allows the servlet to return basic information about itself, such as author, version, and copyright.

    可以看到Servlet接口定义了所有servlet都必须实现的方法。

    下面是Servlet接口中定义的方法:

    其中我们需要注意的是service()方法。它是由servlet容器调用来让servlet对象响应请求。

    2、ServletConfig接口
    ServletConfig接口位于javax.servlet包中。它封装了Servlet的配置信息。在Servlet初始化期间被传递。每一个Servlet对象都有且只有一个ServletConfig对象。

    该接口定义的方法如下:

    3、HttpServletRequest接口
    HttpServletRequest接口位于javax.servlet.http包中,继承了javax.servlet.ServletRequest接口。

    servlet容器产生HttpServletRequest对象,将它作为一个参数传递给service方法(doGet,doPost等)

    该接口定义的方法较多,我们常用的如下:

    4、HttpServletResponse接口
    HttpServletResponse接口位于javax.servlet.http包中,它继承了javax.servlet.ServletResponse接口。

    servlet容器产生HttpServletResponse对象,将它作为一个参数传递给service方法(doGet,doPost等)

    常用的方法如下:

    5、GenericServlet类

    在编写一个Servlet对象时,必须实现javax.servlet.Servlet接口。

    在该接口中包含了5个方法,也就是说创建一个Servlet对象要实现五个方法。为了简化操作,javax.servlet.GenericServlet类实现了Servlet接口。

    GenericServlet类是一个抽象类,分别实现了Servlet接口和ServletConfig接口。该类实现了除service()之外的其他方法。

    在创建Servlet对象时,可以继承GenricServlet类来简化程序中的代码,但需要实现service()类。

    6、HttpServlet类
    javax.servlet.http.HttpServlet类对GenericServlet类进行了扩展。为HTTP请求的处理提供了灵活的方法。

    HttpServlet仍然是一个抽象类。里面实现了service()方法。

    里面对HTTP协议中定义的请求类型提供了相应的方法。

    里面除了对doTrace()和doOptions()有简单的实现之外,其他的方法需要用户自己在继承的类中实现。

  • 相关阅读:
    验证字符串空“” 的表达式
    should not this be a private conversation
    【转】你真的了解word-wrap 和 word-break 的区别吗
    To live is to function,that is all there is in living
    [转] windows 上用程序putty使用 ssh自动登录Linux(Ubuntu)
    Vim/gvim容易忘记的快捷键
    [转] Gvim for windows中块选择的方法
    问题集
    web services
    Trouble Shooting
  • 原文地址:https://www.cnblogs.com/cuglkb/p/6669889.html
Copyright © 2011-2022 走看看