HttpServlet源码解读---重要方法service()
来源:互联网 发布:diy耳放 淘宝 靠谱么 编辑:程序博客网 时间:2024/06/16 07:05
《HttpServlet源码解读—重要方法service()
在SpringMVC中HttpServlet的重要作用:
HttpServlet源码解读:
在这里有两个HTTP字段需要注意,一个是响应实体首部Last-Modified,表示Servlet的修改时间;一个是请求头部,If-Modified-Since,如果资源在指定的时间之后没有修改过,那么表示缓存有效,可以直接使用缓存。HttpServlet.getLastModified()方法用于返回当前内容修改的时间。Service在处理请求时,需要根据其返回值的不同需要做相应的处理:
(1)如果其返回值是-1,则二话不说,直接响应本次请求;
(2)如果是一个正数,且请求头中没有If-Modified-Since字段,或者If-Modified-Since字段表示的时间比getLastModified()返回的之间旧,表示缓存无效,需要处理本次请求。比如,如果当前修改时间是2014年9月1日,请求头中的If-Modified-Since表示的时间是2014年8月1日,也就是说请求中这样说到:如果在2014年8月1日之后没有修改过资源,那么缓存有效。但是很明显,这种情形下,缓存已经失效了。同时把Last-Modified字段写入响应头中
(3)返回值是整数,但是请求头中If-Modified-Since表示的时间新于getLastModified()表示的时间,那么表示缓存有效,服务器直接返回一个304(Not Modified),告诉浏览器直接使用缓存。
这里写代码片import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; 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; /** * 会方便处理HTTP请求的Servlet而提供的抽象类,继承了GenericServlet * 我们写的绝大多数Servlet都应该继承自HttpServlet * HttpServlet根据不同的http请求提供了不同的方法: * doGet:如果servlet支持http GET方法 * doPost:如果servlet支持http POST方法 * 以上两个方法最常用,其他的还有doPut,doDelete,doHead,doOptions,doTrace * * @author Various */ public abstract class HttpServlet extends GenericServlet { private static final long serialVersionUID = 1L; //HTTP的各种方法 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(LSTRING_FILE); /** * 被service()方法调用去处理GET请求 * 重写此方法以支持GET请求同样也自动的支持HEAD请求 * 一个HEAD请求就是一个GET请求,只不过HEAD请求只返回首部,不返回响应实体部分 * 子类如果要支持GET方法,就必须要重写次方法,因为HttpServlet的默认实现是 * 发送错误 */ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //返回使用的协议,protocol/majorVersion,如HTTP/1.1 String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_get_not_supported"); //由于是默认实现,根据不同的协议,直接报错 if (protocol.endsWith("1.1")) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } } /** * 返回的值代表当前输出响应内容的修改时间,总是返回-1,子类可重写 * getLastModified方法是一个回调方法,由HttpServlet的service方法调用, * service方法可以根据返回值在响应消息中自动生成Last_Modified头字段(最后被修改的时间) * Servlet受到一个GET方式的请求时,HttpServlet重载的service方法在调用doGet之前,会先 * 调用本方法,并根据返回值来决定是否调用doGet方法和在响应消息是是否生成Last_Modified字段 * 具体规则如下: * 1.如果是一个负数(本方法默认实现),直接调用doGet方法 * 2.如果是一个正数,且请求消息中没有包含If-Modified-Since请求头,或者请求头中的时间值 * 比返回值旧时,这说明要么是第一次请求,要么是缓存过期了,service将根据返回值生成一个 * Last-Modified字段,并调用doGet方法 * 3.本方法返回值是一个正数,且请求消息中包含的If-Modified-Since的时间值比返回值新或者相同, * 说明缓存有效,service方法将不调用doGet方法,而是返回304(Not Modified)告诉浏览器缓存仍然有效 */ protected long getLastModified(HttpServletRequest req) { return -1; } /** * 没有相应实体,其他与GET方法相同,也正是通过调用doGet来完成请求 */ protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //本类的内部类 NoBodyResponse response = new NoBodyResponse(resp); doGet(req, response); response.setContentLength(); } /** * 也是被service方法调用,默认实现是报错,参考doGet方法 */ 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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } } /** * 被Servlet容器调用,完成请求 * 把请求转发到具体的方法上,通过调用重载的service(HttpServletRequest,HttpServletResponse) * 完成这个方法做的事情就是把request和response转换成HttpServerRequest,HttpServletResponse, * 具体的转发工作由重载的service方法完成 */ @Override 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); } /** * 处理标准的HTTP请求,此方法把具体的请求转发到相应的doXXX方法上 * 被service(ServletRequest,ServletResponse)调用 * 没有理由重写此方法 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //返回HTTP的请求方式,如GET,POST,HEAD String method = req.getMethod(); /** * 如果是GET方式 * 1.首先通过getLastModified(req)获得修改时间 * i.如果修改时间==-1,不管请求是怎样的,直接调用doGet * ii.如果不是-1,则需要获取请求中的If-Modified_Since的值,保存到ifModifiedSince变量中 * 如果请求头中没有If-Modified-Since字段,则ifModifiedSince=-1,通过比较lastModified * 和ifModifiedSince的值来判断缓存是否过期: * 如果lastModified代表的日期比ifModifiedSince代表的日期新时,则说明缓存失效了, * 比如lastModified代表9月1号修改的,ifModifiedSince为8月1号, * 意思是如果自8月1号没有修改过的话,则可以使用缓存,很明显已经修改过了, * 所以不能使用缓存,这个时候要调用doGet方法响应,同时在相应头设置Last-Modified的值。 * 如果lastModified代表的时间比ifModifiedSince旧时,也就是没有修改过, * 则返回304(Not Modified)告诉浏览器,直接使用缓存。 * 2.判断是何种方式,调用具体的doXXX方法 * */ if (method.equals(METHOD_GET)) { long lastModified = getLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); } catch (IllegalArgumentException iae) { // Invalid date header - proceed as if none was set ifModifiedSince = -1; } if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } } } else if (method.equals(METHOD_HEAD)) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp); } else if (method.equals(METHOD_POST)) { doPost(req, resp); } else if (method.equals(METHOD_PUT)) { doPut(req, resp); } else if (method.equals(METHOD_DELETE)) { doDelete(req, resp); } else if (method.equals(METHOD_OPTIONS)) { doOptions(req,resp); } else if (method.equals(METHOD_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(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); } } /* * 设置响应的实体首部字段Lat-Modified */ private void maybeSetLastModified(HttpServletResponse resp, long lastModified) { if (resp.containsHeader(HEADER_LASTMOD)) return; if (lastModified >= 0) resp.setDateHeader(HEADER_LASTMOD, lastModified); } }
Last-Modified 与If-Modified-Since都是用来记录页面的最后修改时间。当客户端访问页面时,服务器会将页面最后修改时间通过 Last-Modified 标识由服务器发往客户端,客户端记录修改时间,再次请求本地存在的cache页面时,客户端会通过 If-Modified-Since 头将先前服务器端发过来的最后修改时间戳发送回去,服务器端通过这个时间戳判断客户端的页面是否是最新的,如果不是最新的,则返回新的内容,如果是最新的,则 返回 304 告诉客户端其本地 cache 的页面是最新的,于是客户端就可以直接从本地加载页面了,这样在网络上传输的数据就会大大减少,同时也减轻了服务器的负担。
- HttpServlet源码解读---重要方法service()
- HttpServlet的两个Service()方法区别
- HttpServlet的两个Service()方法区别
- HttpServlet的两个Service()方法区别
- HttpServlet中的service方法
- HttpServlet中的service方法
- httpservlet 中的两个service方法
- HttpServlet为什么有两个service方法
- HttpServlet类中的两个service()方法
- HttpServlet中的service()、doPost()、doGet()方法解析
- HttpServlet源码
- HttpServlet源码
- Servlet继承HttpServlet重写service方法的实现!
- Apache OFbiz service engine 源码解读
- Apache OFbiz service engine 源码解读
- Apache OFbiz service engine 源码解读
- android IntentService Service HandlerThread 源码解读
- URL类方法源码解读
- network emulator for windows toolkit 网络模拟器
- opencv:计算二维矢量的幅值—magnitude()函数
- RTP协议的封装
- MFC创建窗口菜单
- JVM内存详解Java数据的存储(思维导图整理)
- HttpServlet源码解读---重要方法service()
- opencv:log()函数
- python 基础 —— 获取文件路径
- 临时表空间组+好处
- Android studio的安装及环境配置
- Hadoop之HDFS原理及文件上传下载源码分析(下)
- 经典算法(9)、遗传算法(1):遗传算法简介及基本遗传算法
- opencv:矩阵归一化—normalize()函数
- 复制文本的四种方法