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()有简单的实现之外,其他的方法需要用户自己在继承的类中实现。

Servlet技术——常用的类和接口的更多相关文章

  1. JDBC中常用的类和接口

    <零基础学Java> JDBC中常用的类和接口 利用JDBC的这些类和接口可以更方便地访问并处理存储在数据库中的数据. DriverManager类 DriverManager类 是JDB ...

  2. Hibernate的常用关键类以及接口介绍

    上一篇初步的对Hibernate进行了认识,并测试了Hibernate的HelloWorld, 这里主要介绍HibernateTest类中的相关类和接口,以及其作用和特性,关于Session中的相关方 ...

  3. Java常用的类 包 接口

    类 Byte ShortIntegerLong Float Double Boolean CharFile DateThread(java.lang.ThreadThread类的定义:public c ...

  4. SpringMVC 常用工具类与接口

    ClassPathResource 在类路径下读取资源 public final String getPath()public boolean exists()public InputStream g ...

  5. JavaWeb知识回顾-Servlet常用类、接口及其方法

    今天主要把servlet的一些常用的类.接口和方法简单回顾一下. javax.servlet包 1.javax.servlet.Servlet接口 接口用于开发servlet,所有的servlet都要 ...

  6. 列举Java中常用的包、类和接口

    常用的类: BufferedReader ,BufferedWriter FileReader    ,FileWirter String      ,Integer Date        ,Cla ...

  7. 【Java面试题】43 说出一些常用的类,包,接口,请各举5个

    要让人家感觉你对java ee开发很熟,所以,不能仅仅只列core java中的那些东西,要多列你在做ssh项目中涉及的那些东西.就写你最近写的那些程序中涉及的那些类. 常用的类:BufferedRe ...

  8. Servlet中常用对象及API类之间的关系

    Servlet最常用的对象: 请求对象:ServletRequest和HttpServletRequest,通过该对象获取来自客户端的请求信息 响应对象:ServletResponse和HttpSer ...

  9. JavaWeb中使用到的类与接口整理(一)servlet包

    javaweb学了半本,整理了一下Servlet技术模型.servlet容器模型.jsp技术模型中的类与接口,有助于理解web应用中的页面跳转和参数传递,目录: HttpServlet 可作Scope ...

随机推荐

  1. shell多进程的实现

    需求:多个脚本彼此互不干涉,同时运行,节省时间 菜鸟级实现: #!/bin/sh dir="/data/test" $dir/sbin/test1.sh >> $dir ...

  2. hdu4699-Editor

    Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 发现IDLR四种操作都在光标处发生,且操作完成后光标至多移动1个位置,根据这种“ ...

  3. MongoDB常用方法

    一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...

  4. 前端开发必知必会:CSS Position 全解析

    此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...

  5. Stat2—主成分分析(Principal components analysis)

    最近在猛撸<R in nutshell>这本课,统计部分涉及的第一个分析数据的方法便是PCA!因此,今天打算好好梳理一下,涉及主城分析法的理论以及R实现!come on…gogogo… 首 ...

  6. 【BZOJ】1711: [Usaco2007 Open]Dining吃饭

    [算法]最大流 [题解] S连向食物连向牛连向牛‘连向饮料连向T. 经典的一个元素依赖于两个元素的建图方式. #include<cstdio> #include<algorithm& ...

  7. laravel后台返回ajax数据

    后台模式: $array = array('msg'=>'添加失败!','status'=>'false'); return json_encode($array); 前台显示: $.aj ...

  8. C++面试中可能考察的基础知识(1)

    1 C++中允许函数的嵌套调用,但不允许函数的嵌套定义 2 构建派生类对象时,先调用基类的构造函数,在调用成员对象的构造函数,最后调用派生类构造函数. 3 volatile关键字 volatile提醒 ...

  9. 使用infinite-scroll实现Ghost博文列表的滚动加载

    Ghost博客系统默认提供的博文列表为传统的翻页方式(通过点击上一页.下一页等按钮来切换),随着移动客户端的发展,瀑布流式的滚动加载方式得到广泛应用,有效地提高了用户浏览信息的流畅度.下面详述如何通过 ...

  10. 2017百越杯反序列化writeup

    去年的了,之前也有研究过.只是因为感觉PHP反序列化挺好玩的所以就再研究了一遍.总之感觉反序列化漏洞挺好玩的. 题目代码: <?php class home{ private $method; ...