getLastModified()方法和浏览器缓存

来源:互联网 发布:淘宝闲鱼上拍卖的翡翠 编辑:程序博客网 时间:2024/05/01 00:32

在HttpServlet类中,service负责更具http请求的类型分发给各doXxx()方法处理,源码如下:

protected void service(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException  {    String method = req.getMethod();//根据http请求method的不同作不同处理    if (method.equals("GET")) {       //获取嵌在http请求中的If-Modified-Since参数,这个字段是浏览器最后一次刷新缓存的时间,如果第一次请求就没有       //比较If-Modified-Since参数和lastModified 的新旧,决定是否刷新浏览器缓存。如果lastModified 使用默认的-1L,总是刷新。       //If-Modified-Since参数:服务器制定客户只想要那些自特定日期以来发生改变的页面 lastModified :服务器端返回的东西需要调整的时间点,比如代码  做了修改,或者别的。       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);    }  }



原创粉丝点击