Delete Cookies in Java cookie[] cookies=request.getcookies (); //cookies are not empty, then clearif(cookies!=NULL) {String value=cookies[0].getname (); //Find a user name if(Value.equals ("UserName") ) {cookies[0].setmaxage (0); Response.addcookie (Cookies[i]); }} How to read and write cookies in Java//Write a cookieCookie Namecookie =NewCookie ("name", name); Cookie Passwordcookie=NewCookie ("password"), password); Cookie Optioncookie=NewCookie ("option", "1"); //Life cycleNamecookie.setmaxage (60*60*24*365); Passwordcookie.setmaxage (60*60*24*365); Optioncookie.setmaxage (60*60*24*365); Response.addcookie (Namecookie); Response.addcookie (Passwordcookie); Response.addcookie (Optioncookie); //Read Cookiescookie[] Cookies =request.getcookies (); if(cookies!=NULL) {String name= ""; String Password= ""; String option= ""; for(inti = 0; i < cookies.length; i++) {Cookie C=Cookies[i]; if(C.getname (). Equalsignorecase ("name") ) {Name=C.getvalue (); } Else if(C.getname (). Equalsignorecase ("Password") ) {Password=C.getvalue (); } Else if(C.getname (). Equalsignorecase ("option")) ) {option=C.getvalue (); }}} setmaxage (parameter): A negative number means that the cookie is cleared when the browser is closed, the parameter is 0 o'clock to delete the cookie, and the parameter is positive for how many seconds the cookie exists. Httpservletresponse.addcookie (parameter 1, parameter 2): writes the created cookie to the user's computer. Parameter 1 represents the name of the cookie, and parameter 2 represents the value of the cookie. The Httpservletrequest.getcookies () method allows you to read a list of cookies from the appropriate user, placed in an array of cookie types, through the GetName () and GetValue () of each cookie in the array method to obtain the names and values of individual cookies.
Reprint to: http://blog.csdn.net/ago52030/article/details/1834237
How to read and write cookies in Java