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 ...
随机推荐
- time_t与GMT格式互转
time_t Time::timeFromGMT(string gmt) { char week[4]; memset(week,0,4); char month[4]; memset(month,0 ...
- 如何在WIndows电脑上安装 SVN Server 实现代码版本控制
One 下载-安装 SVN SVNServer 先去官网下载服务器版本的svn server,下载地址 :https://www.visualsvn.com/server/download/ 选 ...
- Spring使用注解方式就行事务管理
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- SSO的几种跨域方案
在此只是记录一下自己在尝试SSO跨域实现的过程中学到的几种跨域方案,不包含任何例子和具体的实现方法. 最近在尝试SSO的跨域,看了好多资料,然后自己记录了一下可以实现的方法: ①跳转所有站点设置coo ...
- parseInt
本文地址:http://www.cnblogs.com/veinyin/p/7647863.html 先来个简单的 console.log(parseFloat("8")); 嗯, ...
- CodeForces - 1004C
Since Sonya is interested in robotics too, she decided to construct robots that will read and recogn ...
- css各种姿势的水平居中
首先是最常用的,利用margin属性的auto来进行水平居中 margin: 0 auto; 其中0是指上下的margin,auto是左右的margin,margin这个属性的简写是按顺时针走的,也就 ...
- perf + 火焰图分析程序性能
1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...
- XGBoost与LightGBM对比分析(转)
尊重原创 来源: https://blog.csdn.net/a790209714/article/details/78086867 XGBoost的四大改进: ①改进残差函数 不用Gini作为残 ...