---restore content starts---
Session
Open a browser to visit a website until the site is closed, the process is called a session
Cookies
A client-side technology that stores the client's resources in response to only the string
Cookies are sent to the browser in the response header
Browser supports 20 cookies per server, a total of 300 each cookie 4k size
To implement the last time the client access is logged
1 Get client-owned cookies
Reponse.setcontettype ("Text/html;charset=utf-8");
PrintWriter pw= Reponse.getwriter ();
Cookie [] cookies=resques.getcookies ();
2 Find the cookie that stores the last login time from the cookie
if (cookies!=null)
{
for (int i=0;i<cookies.length;i++)//cookie is a key-value pair, which is a string-type
{String name = Cookies[i].getname (); Get the name of a cookie
if ("LastAccessTime". Equals (name))
String value =cookies[i].getvalue (); Value at this time is the number of milliseconds
Pw.wirte (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date (Long.parselong)));//Time formatting
}
}
The client that sent the time of the login as a cookie
Cookie cookie= New Cookie ("LastAccessTime", System.currenttimemillis () + ""); Must be of type string
Send cookies to clients
Reponse.addcookie (cookie);
Cookies are stored in the browser's memory by default, and once the browser is closed, the stored cache is erased
Use Setmaxage to set cookie survival time, default unit is seconds
Setmaxage (-1) default is-1 delete at end of session
0 when cookies are not saved
Positive number is life cycle time
What conditions are met the client sends a cookie to the server side.
1 The resource path being accessed, the previous segment to match.
Cookie.setpath (Request.getcontextpath ());
Session
One client corresponds to a session, and only one sessions object in the same conversation
SetAttribute () GetAttribute () implements session storage
On the server side of a technology, session can save objects
Session.setmaxinactiveinterval (10);//Set inactive time 10s automatic destruction;
Request.getsession (true); the existence of a session using the existing, does not exist will create new;
Request.getsession (false); The existence of a session using the presence of a nonexistent will not be created.
2016.9.13 Session Learning