Today we are dealing with a need to delete cookies. I added the query criteria to the cookie when I queried the page, so that the next time I had the query, I could bring the condition of the previous query. However, you need to delete all cookies when you switch the organization and log out.
Here to illustrate the problem, just embed part of the Code @requestmappingpublic modelandview index (@RequestParam (value = " ChannelName ", required = false) string channelname, @RequestParam (value = " Channelstatus ", required = false) string channelstatus, @RequestParam (value = "MediaID", required = false) long mediaid, @RequestParam (value = " PageIndex ", required = false) integer pageindex, @RequestParam (value = " PageSize ", required = false) Integer pageSize,HttpSession session, Httpservletrequest request,httpservletresponse response) {// Get the parameter from the cookie if the request parameter is empty If the request parameter is not empty, it is saved in the write cookie, the parameter/* * operation is provided for the next query cookie--if there is no condition, the condition * rule is read from the cookie: Read from the cookie when the condition is all empty. When the condition has a non-empty * read condition and is saved to the cookie . */if (channelname == Null && channelstatus == null && pageindex == null&& pagesize == null & & mediaid == null) {for (Cookie cookie : request.getcookies ()) {if (Cookie.getname () equalsignorecase ("Pmc.channelname") {channelName = Java.net.URLDecoder.decode (Cookie.getvalue ());} if (Cookie.getname (). Equalsignorecase ("Pmc.channelstatus")) {channelStatus = Cookie.getvalue ();} if (Cookie.getname (). Equalsignorecase ("Pmc.pageindex")) {pageindex = integer.valueof ( Cookie.getvalue ());} if (Cookie.getname (). Equalsignorecase ("Pmc.pagesize")) {pagesize = integer.valueof ( Cookie.getvalue ());} if (Cookie.getname (). Equalsignorecase ("Pmc.mediaid")) {mediaid = long.valueof ( Cookie.getvalue ());}}} else {if (channelname != null) {response.addcookie (New cookie (" Pmc.channelname ", java.net.urlencoder.encode (ChannElname)));} else{ //the code to save the cookie. Cookie2.setmaxage (0); Set the cookie to expire when the browser is closed cookie cookie2=new Cookie ("Pmc.channelname", null); cookie2.setmaxage (0); Response.addcookie (cookie2);} if (channelstatus != null) {response.addcookie (New cookie ("Pmc.channelStatus", Channelstatus));} else{ //the code to save the cookie. Cookie2.setmaxage (0); Set the cookie to expire when the browser is closed cookie cookie2=new Cookie ("Pmc.channelstatus", null); cookie2.setmaxage (0); Response.addcookie (cookie2);} if (pageindex != null) {response.addcookie (New cookie ("Pmc.pageIndex", Pageindex.tostring ()));} if (pagesize != null) {response.addcookie (New cookie ("Pmc.pagesize", Pagesize.tostring ()));} if (mediaid != null) {response.addcookie (New cookie ("Pmc.mediaid", MediaId.toString ()) );} else{ //the code to save the cookie. Cookie2.setmaxage (0); Set the cookie to expire when the browser is closed cookie cookie2=new &nbSp Cookie ("Pmc.mediaid", null); cookie2.setmaxage (0); Response.addcookie (Cookie2);}}
My goal is to remove this cookie from the login. There are a number of cookie values that can be found in every index.do query throughout the system. All I have to do is get all the cookies and delete them when I quit. I did it.
/** execution exits the */@RequestMapping (value = "/logout") public modelandview logout ( Httpservletrequest request,httpservletresponse response,httpsession session) { // get cookies Array cookie[] cookies = request.getcookies (); // iterate to find and clear cookie for (Cookie cookie: cookies) { // Remove Cookie.setmaxage (0) to delete cookie. cookie.setmaxage (0); cookie.setpath ("/"); response.addcookie (Cookie); }// Modify User login log authorizeduser authorizeduser = (authorizeduser) session.getattribute ("Authorizeduser"); Logservice.updateloginlog (Authorizeduser.getloginlogid ());// Destroy User sessionsession.invalidate ();// Jump to Home Modelandview mv = new modelandview (" redirect:/"); return mv;}
In the process, however, problems were found. When you exit, using Request.getcookies () to obtain all cookies, you will always be able to obtain only one system-generated cookie. Those cookies I set myself manually are not available at all.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/78/wKioL1XRjauBF7flAAQViDNiTU4945.jpg "title=" 2.png " alt= "Wkiol1xrjaubf7flaaqvidnitu4945.jpg"/>
After access to data and tests. Finally found the cause of the problem. The reason is in Cookie2.setmaxage (0); Look at how we set it up:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/7B/wKiom1XRh__g9KrGAAQnhZwWi1o988.jpg "title=" 1.png " alt= "Wkiom1xrh__g9krgaaqnhzwwi1o988.jpg"/>
will Cookie2.setmaxage (0); Delete the current cookie. So you can't read this cookie anywhere else. Because I did not set Cookie.setpath () and Cookie.setdomain (). All did not implement the actual deletion, why did not delete it, you can refer to this article: http://tompig.iteye.com/blog/714295
Find a solution by reviewing the data and testing:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/78/wKioL1XRjiDQ0XJVAAHDiBe9x_8580.jpg "title=" 3.png " alt= "Wkiol1xrjidq0xjvaahdibe9x_8580.jpg"/>
The Set Cookie2.setmaxage (-1) does not really mean that the browser is closed and the cookie is invalid.
Finally found out why you can't get a cookie when you sign out.
Now that's the problem again, I can't really implement cookie deletion. This question is explained more on the Internet.
Online, deleting cookies is a must to set SetPath () and SetDomain () two parameters. Tested personally: I only set the SetPath () to delete the cookie.
When adding a cookie, set SetPath ();
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/7B/wKiom1XRkWDgb2BKAADundZHYQM153.jpg "title=" 5.png " alt= "Wkiom1xrkwdgb2bkaadundzhyqm153.jpg"/>
When you delete a cookie, you also set SetPath ();
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/7B/wKiom1XRkcGAQQjlAAJy8IpH5yo973.jpg "title=" 6.png " alt= "Wkiom1xrkcgaqqjlaajy8iph5yo973.jpg"/>
The problem was finally solved.
Use Request.getcookies to delete all cookies in Java