HttpServlet---getLastModified与缓存

来源:互联网 发布:sql截取指定字符前面 编辑:程序博客网 时间:2024/04/30 18:56

今天看HttpServlet源码看到一个函数getLastModified,上网查阅与浏览器缓存有关系,根据源码,画了个流程图理解下:
源码:

    protected void service(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException    {    String method = req.getMethod();    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 = req.getDateHeader(HEADER_IFMODSINCE);        if (ifModifiedSince < lastModified) {            // 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);        }        }    }     }

这里写图片描述

最后的结果为只有当request中存储的服务器上次告诉它的最后修改时间比服务器端request的修改时间later时,服务器会响应304状态码,浏览器解析该状态码,明白请求并未改变,启用浏览器中缓存的数据。

0 0