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

Servlet技术——常用的类和接口的更多相关文章
- JDBC中常用的类和接口
<零基础学Java> JDBC中常用的类和接口 利用JDBC的这些类和接口可以更方便地访问并处理存储在数据库中的数据. DriverManager类 DriverManager类 是JDB ...
- Hibernate的常用关键类以及接口介绍
上一篇初步的对Hibernate进行了认识,并测试了Hibernate的HelloWorld, 这里主要介绍HibernateTest类中的相关类和接口,以及其作用和特性,关于Session中的相关方 ...
- Java常用的类 包 接口
类 Byte ShortIntegerLong Float Double Boolean CharFile DateThread(java.lang.ThreadThread类的定义:public c ...
- SpringMVC 常用工具类与接口
ClassPathResource 在类路径下读取资源 public final String getPath()public boolean exists()public InputStream g ...
- JavaWeb知识回顾-Servlet常用类、接口及其方法
今天主要把servlet的一些常用的类.接口和方法简单回顾一下. javax.servlet包 1.javax.servlet.Servlet接口 接口用于开发servlet,所有的servlet都要 ...
- 列举Java中常用的包、类和接口
常用的类: BufferedReader ,BufferedWriter FileReader ,FileWirter String ,Integer Date ,Cla ...
- 【Java面试题】43 说出一些常用的类,包,接口,请各举5个
要让人家感觉你对java ee开发很熟,所以,不能仅仅只列core java中的那些东西,要多列你在做ssh项目中涉及的那些东西.就写你最近写的那些程序中涉及的那些类. 常用的类:BufferedRe ...
- Servlet中常用对象及API类之间的关系
Servlet最常用的对象: 请求对象:ServletRequest和HttpServletRequest,通过该对象获取来自客户端的请求信息 响应对象:ServletResponse和HttpSer ...
- JavaWeb中使用到的类与接口整理(一)servlet包
javaweb学了半本,整理了一下Servlet技术模型.servlet容器模型.jsp技术模型中的类与接口,有助于理解web应用中的页面跳转和参数传递,目录: HttpServlet 可作Scope ...
随机推荐
- VS Code 配置删除左边单词快捷键(同Sublime 和 Atom)
VS Code 中删除一行的快捷键默认是 cmd + shift + k,或者使用简介 cmd + x,对于一个长期使用 Sublime 和 Atom 的程序猿来说,在VS Code 上删除行,特别是 ...
- [Thu Summer Camp 2015]解密运算
4104: [Thu Summer Camp 2015]解密运算 Time Limit: 10 Sec Memory Limit: 512 MB Description 对于一个长度为N的字符串,我 ...
- 「七天自制PHP框架」应用:Model外键链接
这里以行政区数据为例: 一级行政区数据范例: 二级行政区范例: 三级行政区范例: 在Model层建立三个Model class ProvinceModel extends Model{ public ...
- 不用注解添加controller抛出No adapter for handler异常
不用注解添加controller时会抛出No adapter for handler异常. 解决方法:在DispatcherServlet的配置文件(***-servlet.xml)中加入如下两行: ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields
Different query operators in MongoDB treat null values differently. The examples on this page use th ...
- 【洛谷 P2147】 [SDOI2008]洞穴勘测(LCT)
题目链接 LCT裸题.. #include <cstdio> #define R register int #define I inline void #define lc c[x][0] ...
- Metasploit 一些重要模块使用介绍
本文是"T00LS Metasploit(第一季)"的文档版,是个人在观看视频动手操作的一个记录,仅供学习.文中会介绍Metasploit的一些基本使用:端口扫描.smb扫描.服务 ...
- JSTL标签库笔记
1. 概述 JSTL(Jsp Standard Tag Library)即JSP标准标签库,只能运行在支持JSP1.2↑和Servlet2.3↑规范的容器上. 通常情况下我们在编写JSP页面的时候,在 ...
- bzoj 1072 状压DP
我们用w[i][j]来表示,i是一个二进制表示我们选取了s中的某些位,j表示这些位%d为j,w[i][j]则表示这样情况下的方案数,那么我们可以得到转移.w[i|(1<<k)][(j*10 ...
- idea 控制台乱码
第一步:修改intellij idea配置文件: 找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别 ...