First, the cookie is the server side to write the identifier to the browser, the browser access server automatically carry
Server-side Cookie creation
Random random = new random (); Cookie cookie = new Cookie ("Writecookie", "Writecookie" +random.nextint); Cookie.setmaxage (60);//Set Browser cookie lifetime , in Seconds//cookie.setpath (Request.getcontextpath ()); Cookie.setpath ("/");//Set the scope of the cookie to the entire site Response.addcookie ( Cookies);
Server gets the cookie of the browser
cookie[] cookies = request.getcookies (), if (Cookies!=null) {for (Cookie cookie:cookies) {out.println (Cookie.getname () + ":" +cookie.getvalue ()); Out.println ("<br>");}}
Second, the application of the cookie scenario
1. Automatic User Login
2, browse the latest products
This article is from the "11137669" blog, please be sure to keep this source http://11147669.blog.51cto.com/11137669/1905844
Cookie for Java Web