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!