1.Cookie Technology:
Client Technology
The cookie is based on the Set-cookie response header and cookie request header, the server can send Set-cookie request header command Browser to save a cookie information, the browser will access the server as a cookie request header to bring back the previously saved information
Request.getcookies ();
Response.addcookie (Cookie c);
The new cookie (String name,string value)//cookie needs to set the name and value of the cookie when it is constructed.
GetName ();
GetValue ();
SetValue ();
!! Setmaxage and Getmaxage method
--a cookie is a session-level cookie if it is not set maxage, and the cookie is stored in the browser's memory after it is called by the browser. This means that as long as the browser is turned off, the cookie information disappears as the browser memory is destroyed. A cookie can also be set to MaxAge, browsing once a cookie is found to have been set MaxAge, The cookie information is saved as a file in the browser's temporary folder and saved to the specified time. This way, even if you switch browsers many times, because these browsers can see the cookie file in the Temp folder, Therefore, the cookie information is present before the cookie expires.
--To command the browser to delete a cookie, send a cookie,maxage with the same name as path, set to 0, the browser to identify the cookie by name +path, found that the same name and Path,cookie overwrite immediately after the time-out is deleted, thereby deleting the cookie.
!! SetPath and GetPath method
--Used to tell the browser which path and its sub-path to the server are being accessed with the current cookie information.
If not explicitly set, the default path is the path to the servlet where the cookie is sent
http://localhost/Day05/servlet/...
SetDomain and GetDomain method
--Used to inform the browser of which domain name to visit with the current cookie information. However, it is important to note that a modern browser will refuse to accept this cookie once it has found that the cookie has set the domain information. We don't usually set this method
2. Experiment:
Package Cn.itheima.cookie;import Java.io.ioexception;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 CookieServlet Extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//Set encoding Response.setcontenttype ("Text/html;charset=utf-8"); cookie[] cookies = request.getcookies (); Cookie FINDC = null;//traversal cookiesif (cookies!=null) {for (Cookie c:cookies) {if ("last". Equals (C.getname ())) {findc=c;}} if (findc==null) {response.getwriter (). Write ("You are the first access, the current time is:" +new date (). toLocaleString ());} Else{response.getwriter (). Write ("The time you last visited was:" +findc.getvalue () + "<br/>" + "Current time is:" +new date (). toLocaleString ( ));} Send the current time as a cookie to the browser-the lifetime is session-level by default, which is the session end cookie death date date=new date (); Cookie Last=new Cookie ("Last", date.tolocalestring ()); Response.addcookie (last);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Operation Result:
Dark Horse DAY05 Cookie Technology Introduction & record last visited time