HttpServlet---getlastmodified and caching

Source: Internet
Author: User

The code for overriding the service method in HttpServlet is as follows:

   protected voidService (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {String meth OD=Req.getmethod (); if(Method.equals (method_get)) {LongLastModified =getlastmodified (req); if(LastModified = =-1) {doget (req, resp); } Else {        LongIfmodifiedsince =Req.getdateheader (header_ifmodsince); if(Ifmodifiedsince <lastmodified) {       maybesetlastmodified (resp, lastmodified);        Doget (req, resp); } Else{resp.setstatus (httpservletresponse.sc_not_modified); }        }    }     }

What is "last-modified"? and If-modified-since?
When the browser requests a URL for the first time, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute that marks the last time the file was modified at the end of the service period, similar in format:

Last-modified:fri, 2006 18:53:33 GMT

When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:

If-modified-since:fri, 2006 18:53:33 GMT

If the server-side resource does not change, the HTTP 304 (not Changed.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.

HttpServlet---getlastmodified and caching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.