Java cookie browser cache clearing solution!

Source: Internet
Author: User
Tags browser cache
Package CN. csdn. web. cookie; import Java. io. ioexception; import Java. io. printwriter; import Java. util. date; import javax. servlet. servletexception; import javax. servlet. HTTP. cookie; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class democookie extends httpservlet {public void doget (httpservletrequest request, httpser Vletresponse response) throws servletexception, ioexception {// set the encoding method response. setcharacterencoding ("UTF-8"); // sets the header file, encoded as a UTF-8response.setHeader ("Content-Type", "text/html; charset = UTF-8"); // printwriter out = response. getwriter (); out. print ("your last visit was:"); cookie Cookies [] = request. getcookies (); For (INT I = 0; cookies! = NULL & I <cookies. length; I ++) {cookie = Cookies [I]; If ("lasttime ". equals (cookie. getname () {string lasttime = cookie. getvalue (); Date = new date (Long. parselong (lasttime); out. println (date. tolocalestring () ;}/// record this cookie information record this access time // create cookiecookie ck = new cookie ("lasttime", system. currenttimemillis () + ""); // new cookie ("lasttime", system. currenttimemillis (); // The effective date is 0 // ck. setmaxage (3600); // ck. setpath ("/20111024/servlet/cookiedemo"); // The Response Message sent to you. addcookie (CK);} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {This. doget (request, response );}}
 

This demo is to study the cache of Java Cookie Web pages. Let's talk about how to clear the cache information when the IE browser is closed!

The following describes how to use the setmaxage () method in the cookie class,

The setmaxage () method sets the cookie survival time. Here, I set 3600 seconds, that is, 1 hour's survival time,

Note: The unit here is second, that is, 3600 seconds!

Therefore, if we need to set the cookie survival time, we can use this method. We can set the parameter );

In this case, the cookie is automatically cleared after the browser is closed!

========================================================== ======================================

Add QQ: 284651035 (this blogger) for help)

Weibo: http://weibo.com/cnbailu (Bai Lu)

Http://weibo.com/u/2717396075 (featured jokes)

Mr Chen Weibo: http://weibo.com/redarmychen Java direction

Miss Li Weibo: http://weibo.com/csdnliwei PHP direction

Mobile (mobile) Software Development Exchange Group: 57636364(Please note that the application is on the csdn blog. Remember to change your business card after entering the group.) Thank you!

Related Article

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.