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 ...
随机推荐
- JNI实现JAVA和C++互相调用
SDK.h #ifndef SDK_H #define SDK_H #include "AsyncProxy.h" #include "Module.h" #i ...
- springboot+spring session+redis+nginx实现session共享和负载均衡
环境 centos7. jdk1.8.nginx.redis.springboot 1.5.8.RELEASE session共享 添加spring session和redis依赖 <depen ...
- DialogFragment 将数据传回Activity的onActivityResult方法
在MyActivity中 弹出一个DialogFragment (某一个控件的点击事件) search= findViewById(R.id.search); search.setOnClickLis ...
- java用于控制可见性的4个访问修饰符
仅对本类可见——private 对所有类可见——public 对本包的所有子类可见——protected 对本包可见——默认(很遗憾)不需要修饰符
- 开发技巧:高效的使用 Response.Redirect
我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortException. 这 ...
- jQuery网格插件 ParamQuery
ParamQuery是一种轻量级的jQuery网格插件,基于用于用户界面控制.具有一致API的优秀设计模式jQueryUI Widget factory创建,能够在网页上展示各种类似于Excel和Go ...
- jQuery代码优化:基本事件
jQuery对事件系统的抽象与优化也是它的一大特色.本文仅从事件系统入手,简要分析一下jQuery为什么提供mouseenter和mouseleave事件,它们与标准的mouseover.mouseo ...
- CentOS 怎么设置某个目录包括子目录的写入权限 777呢
chmod -R 777 文件夹例如:chmod -R 777 /var var的权限就变成777,var下的所有子目录和文件权限都将变成777
- iOS 程序启动流程
iOS程序启动原理 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong iOS应用程序运行 ...
- 【译】Attacking XML with XML External Entity Injection (XXE)
原文链接:Attacking XML with XML External Entity Injection (XXE) XXE:使用XML外部实体注入攻击XML 在XML中,有一种注入外部文件的方式. ...