HttpServlet---getLastModified與緩衝

來源:互聯網
上載者:User

標籤:date   let   hang   last   change   style   odi   exception   logs   

在HttpServlet中重寫service方法的代碼如下:

   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) {        doGet(req, resp);        } else {        long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);        if (ifModifiedSince < lastModified) {                   maybeSetLastModified(resp, lastModified);            doGet(req, resp);        } else {            resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);        }        }    }     }

 

什麼是”Last-Modified”?和 If-Modified-Since?
        在瀏覽器第一次請求某一個URL時,伺服器端的返回狀態會是200,內容是你請求的資源,同時有一個Last-Modified的屬性標記此檔案在服務期端最後被修改的時間,格式類似這樣:

        Last-Modified: Fri, 12 May 2006 18:53:33 GMT

        用戶端第二次請求此URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送 If-Modified-Since 前序,詢問該時間之後檔案是否有被修改過:

        If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT

        如果伺服器端的資源沒有變化,則自動返回 HTTP 304 (Not Changed.)狀態代碼,內容為空白,這樣就節省了傳輸資料量。當伺服器端代碼發生改變或者重啟伺服器時,則重新發出資源,返回和第一次請求時類似。從而保證不向用戶端重複發出資源,也保證當伺服器有變化時,用戶端能夠得到最新的資源。

 

HttpServlet---getLastModified與緩衝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.