Servlet--HttpServlet类
- HttpServlet类
定义
public class HttpServlet extends GenericServlet implements Serializable
这是一个抽象类,用来简化 HTTP Servlet 写作的过程。它是 GenericServlet 类的扩充,提供了一个处理 HTTP 协议的框架。在这个类中的 service 方法支持例如 GET、POST 这样的标准的 HTTP 方法。这一支持过程是通过分配他们到适当的方法(例如 doGet、doPost)来实现的。
方法
1、doDelete
protected void doDelete(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
被这个类的 service 方法调用,用来处理一个 HTTP DELETE 操作。这个操作允许客户端请求从服务器上删除 URL。这一操作可能有负面影响,对此用户就负起责任。这一方法的默认执行结果是返回一个HTTP BAD_REQUEST 错误。 当你要处理 DELETE
请求时,你必须重载这一方法。
2、doGet
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
被这个类的 service 方法调用,用来处理一个 HTTP GET 操作。这个操作允许客户端简单地从一个 HTTP 服务器“获得”资源。对这个方法的重载将自动地支持 HEAD 方法。GET 操作应该是安全而且没有负面影响的。这个操作也应该可以安全地重复。这一方法的默认执行结果是返回一个 HTTP BAD_REQUEST 错误。
3、doHead
protected void doHead(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
被这个类的 service 方法调用,用来处理一个 HTTP HEAD 操作。默认的情况是,这个操作会按照一个无条件的 GET 方法来执行,该操作不向客户端返回任何数据,而仅仅是返回包含内容长度的头信息。与 GET 操作一样,这个操作应该是安全而且没有负面影响的。这个操作也应该可以安全地重复。这个方法的默认执行结果是自动处理 HTTP HEAD 操作, 这个方法不需要被一个子类执行。
4、doOptions
protected void doOptions(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
被这个类的 service 方法调用,用来处理一个 HTTP OPTION 操作。这个操作自动地决定支持哪一种 HTTP 方法。例如,一个 Servlet 写了一个 HttpServlet 的子类并重载了 doGet方法,doOption 会返回下面的头:Allow:GET,HEAD,TRACE,OPTIONS
你一般不需要重载这个方法。
5、doPost
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,
IOException;
被这个类的 service 方法调用, 用来处理一个 HTTP POST 操作。 这个操作包含请求体的数据,Servlet 应该按照他行事。这个操作可能有负面影响。例如更新存储的数据或在线购物。这一方法的默认执行结果是返回一个 HTTP BAD_REQUEST 错误。当你要处理 POST操作时,你必须在 HttpServlet 的子类中重载这一方法。
6、doPut
protected void doPut(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
被这个类的 service 方法调用, 用来处理一个 HTTP PUT 操作。 这个操作类似于通过 FTP发送文件。这个操作可能有负面影响。例如更新存储的数据或在线购物。这一方法的默认执行结果是返回一个 HTTP BAD_REQUEST 错误。当你要处理 PUT 操
作时,你必须在 HttpServlet 的子类中重载这一方法。
7、doTrace
protected void doTrace(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
被这个类的 service 方法调用,用来处理一个 HTTP TRACE 操作。这个操作的默认执行结果是产生一个响应,这个响应包含一个反映 trace 请求中发送的所有头域的信息。当你开发 Servlet 时,在多数情况下你需要重载这个方法。
8、getLastModified
protected long getLastModified(HttpServletRequest request);
返回这个请求实体的最后修改时间。为了支持 GET 操作,你必须重载这一方法,以精确地反映最后修改的时间。 这将有助于浏览器和代理服务器减少装载服务器和网络资源, 从而更加有效地工作。返回的数值是自 1970-1-1 日(GMT)以来的毫秒数。默认的执行结果是返回一个负数,这标志着最后修改时间未知,它也不能被一个有条件的GET 操作使用。
9、service
protected void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException;
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException;
这是一个 Servlet 的 HTTP-specific 方案, 它分配请求到这个类的支持这个请求的其他方法。当你开发 Servlet 时,在多数情况下你不必重载这个方法。实际开发中需要重载的就是doGet和doPost这2个方法。然后一般都是重写doPost方法,然后重写doGet的时候只需要调下doPost方法即可。下面贴出HttpServlet类的源码。
package javax.servlet.http; import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public abstract class HttpServlet extends GenericServlet
implements Serializable
{
private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_HEAD = "HEAD";
private static final String METHOD_GET = "GET";
private static final String METHOD_OPTIONS = "OPTIONS";
private static final String METHOD_POST = "POST";
private static final String METHOD_PUT = "PUT";
private static final String METHOD_TRACE = "TRACE";
private static final String HEADER_IFMODSINCE = "If-Modified-Since";
private static final String HEADER_LASTMOD = "Last-Modified";
private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";
private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings"); protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1"))
resp.sendError(405, msg);
else
resp.sendError(400, msg);
} protected long getLastModified(HttpServletRequest req)
{
return -1L;
} protected void doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
NoBodyResponse response = new NoBodyResponse(resp); doGet(req, response);
response.setContentLength();
} protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1"))
resp.sendError(405, msg);
else
resp.sendError(400, msg);
} protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_put_not_supported");
if (protocol.endsWith("1.1"))
resp.sendError(405, msg);
else
resp.sendError(400, msg);
} protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_delete_not_supported");
if (protocol.endsWith("1.1"))
resp.sendError(405, msg);
else
resp.sendError(400, msg);
} private static Method[] getAllDeclaredMethods(Class c)
{
if (c.equals(HttpServlet.class)) {
return null;
} Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass());
Method[] thisMethods = c.getDeclaredMethods(); if ((parentMethods != null) && (parentMethods.length > 0)) {
Method[] allMethods = new Method[parentMethods.length + thisMethods.length]; System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length); System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length); thisMethods = allMethods;
} return thisMethods;
} protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Method[] methods = getAllDeclaredMethods(getClass()); boolean ALLOW_GET = false;
boolean ALLOW_HEAD = false;
boolean ALLOW_POST = false;
boolean ALLOW_PUT = false;
boolean ALLOW_DELETE = false;
boolean ALLOW_TRACE = true;
boolean ALLOW_OPTIONS = true; for (int i = 0; i < methods.length; i++) {
Method m = methods[i]; if (m.getName().equals("doGet")) {
ALLOW_GET = true;
ALLOW_HEAD = true;
}
if (m.getName().equals("doPost"))
ALLOW_POST = true;
if (m.getName().equals("doPut"))
ALLOW_PUT = true;
if (m.getName().equals("doDelete")) {
ALLOW_DELETE = true;
}
}
String allow = null;
if ((ALLOW_GET) &&
(allow == null)) allow = "GET";
if (ALLOW_HEAD)
if (allow == null) allow = "HEAD"; else
allow = allow + ", HEAD";
if (ALLOW_POST)
if (allow == null) allow = "POST"; else
allow = allow + ", POST";
if (ALLOW_PUT)
if (allow == null) allow = "PUT"; else
allow = allow + ", PUT";
if (ALLOW_DELETE)
if (allow == null) allow = "DELETE"; else
allow = allow + ", DELETE";
if (ALLOW_TRACE)
if (allow == null) allow = "TRACE"; else
allow = allow + ", TRACE";
if (ALLOW_OPTIONS) {
if (allow == null) allow = "OPTIONS"; else
allow = allow + ", OPTIONS";
}
resp.setHeader("Allow", allow);
} protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String CRLF = "\r\n";
String responseString = "TRACE " + req.getRequestURI() + " " + req.getProtocol(); Enumeration reqHeaderEnum = req.getHeaderNames(); while (reqHeaderEnum.hasMoreElements()) {
String headerName = (String)reqHeaderEnum.nextElement();
responseString = responseString + CRLF + headerName + ": " + req.getHeader(headerName);
} responseString = responseString + CRLF; int responseLength = responseString.length(); resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(responseString);
out.close();
} protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod(); if (method.equals("GET")) {
long lastModified = getLastModified(req);
if (lastModified == -1L)
{
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if (ifModifiedSince < lastModified / 1000L * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(304);
}
}
}
else if (method.equals("HEAD")) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
}
else if (method.equals("POST")) {
doPost(req, resp);
}
else if (method.equals("PUT")) {
doPut(req, resp);
}
else if (method.equals("DELETE")) {
doDelete(req, resp);
}
else if (method.equals("OPTIONS")) {
doOptions(req, resp);
}
else if (method.equals("TRACE")) {
doTrace(req, resp);
}
else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(501, errMsg);
}
} private void maybeSetLastModified(HttpServletResponse resp, long lastModified)
{
if (resp.containsHeader("Last-Modified"))
return;
if (lastModified >= 0L)
resp.setDateHeader("Last-Modified", lastModified);
} public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try
{
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
}
Servlet--HttpServlet类的更多相关文章
- Servlet接口、GenericServlet类、HttpServlet类
Servlet是最顶层的接口,其提供的方法有: init(ServletConfig config):void // 初始化 getServletConfig():ServletConfig // 取 ...
- Servlet-通过继承HttpServlet类实现Servlet程序
通过继承HttpServlet类实现Servlet程序(开发一般用) 一般在实际项目开发中,都是使用继承 HttpServlet类的方式实现Servlet程序 1,编写一个类去继承 HttpServl ...
- Eclipse操作Servlet入门案例,创建类无法实现Servlet接口和继承HttpServlet类
问题描述: 从昨天下午到今天中午,这个问题被缠绕了良久.百度了很多,却都没有一言命中要害. 首先,还是基于对Web的理解. 第一:建立的是Dynamic Web Project: 第二:然后需要 ...
- Tomcat是如何将请求一步步传递到我们编写的HttpServlet类中的
我们平常编写好的HttpServlet类后,就可以处理请求了,但是服务器在接收到请求信息以后是如何将这些请求传递到我们编写的Servlet类中的???这个疑问在我心中的已经很久了,现在要来解决它. 我 ...
- JavaWeb知识回顾-Servlet常用类、接口及其方法
今天主要把servlet的一些常用的类.接口和方法简单回顾一下. javax.servlet包 1.javax.servlet.Servlet接口 接口用于开发servlet,所有的servlet都要 ...
- xxxservlet继承HttpServlet类
"HttpServlet类被定义为抽象类,但是源码里面没有抽象方法.所以没有一定要求实现的方法.之所以定义为抽象类,是因为他继承了GenericServlet这个抽象类.并没有全部实现里面的 ...
- 使用 acl_cpp 的 HttpServlet 类及服务器框架编写WEB服务器程序(系列文章)
在 <用C++实现类似于JAVA HttpServlet 的编程接口 > 文章中讲了如何用 HttpServlet 等相关类编写 CGI 程序,于是有网友提出了 CGI 程序低效性,不错, ...
- 【HttpServlet】HttpServlet类
创建时间:6.15 HttpServlet 但在实际开发中,我们不会直接去实现Servlet接口,因为那样需要覆盖的方法太多, 我们一般创建类继承HttpServlet 实现步骤: 1)创建类继 ...
- httpservlet类中两个service方法
在浏览器访问html页面时,当数据提交给servlet时发生了什么,这是我们需要了解的. 1.我们需要了解一下servlet的继承体系. servlet接口 ------->GenericSer ...
- 【android学习2】:Eclipse中HttpServlet类找不到
Eclipse中使用的HttpServlet类之所以识别不到的原因是没有导入Servlet-api.jar包,这个包在所安装在的tomcat的lib文件下,所以只需要导入即可. 在需要导入的工程上右键 ...
随机推荐
- HTML知识点总结之<a>标签
HTML是什么? HTML(Hyper Text Markup Language)超文本标记语言,用来描述网页的一种语言.超文本是指网页不止有文本,还可以有图片,链接,视频,音频等非文本元素.标记语言 ...
- 搬个小板凳,我们扯扯Docker的前生
一.新瓶装旧酒 首先我们需要知道,Docker是一个"箩筐": 1.存储:Device Mapper.BtrFS.AUFS 2.名字空间:UTS.IPC.Mount.PID.Net ...
- Python编程笔记 - 列表
这篇文章开始介绍Python中的容器.Python容器包括列表.元组.集合与字典.这些数据结构中都涉及到很多的方法,这里对比较常用的一些方法进行介绍,不用每个方法都记住,熟悉常用的即可. 首先,我们先 ...
- CCS入门基础
1.CSS概念 全称为Cascading Style Sheets(层叠样式表),支持专有的文件 - 扩展名为".css" 作用:将HTML的结构(HTML标签即html)与样 ...
- windbg关于.NET分析的扩展命令
收到一个dump文件,运行环境的.net framework的详细版本是多少呢? dump信息与性能计数器结合分析时,想知道该dump运行的进程号是多少? dump定位到有效的堆栈信息,而对应的源码是 ...
- 列表(List) 的增删改查及其他方法 和元组(tuple)的查
一.列表 1.列表简介: 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:li ...
- 洛谷 P3410 拍照
洛谷 P3410 拍照 题目描述 小B有n个下属,现小B要带着一些下属让别人拍照. 有m个人,每个人都愿意付给小B一定钱让n个人中的一些人进行合影.如果这一些人没带齐那么就不能拍照,小B也不会得到钱. ...
- bzoj 4918: 回文数对
传送门 Description 给定区间[L,R],请统计有多少对整数A,B(L<=A,B<=R)满足A xor B的值在二进制表示下,去掉所有前导0后是回文串 Input 第一行包含一个 ...
- HDU4355-Party All the Time-三分
Party All the Time Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- poj_2115C Looooops(模线性方程)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...