protected void doget (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException {
//TODO auto-generated Method Stub
//Set server side to output UTF-8 encoding
response.setcharacterencoding ("UTF-8 ");
//Set the browser to receive UTF-8 encoding to solve the Chinese garbled problem
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
//Get the array of cookies passed by the browser when accessing the server
cookie[] cookies = request.getcookies ();
//If the user is the first time access, then the resulting cookie will be null
if (cookie = null) {
Out.write ("The time you last visited:");
for (int i = 0; i < cookies.length; i++) {
Cookie Cook ie = Cookies[i];
if (Cookie.getname () equals ("LastAccessTime")) {
Long lastaccesstime = Long.parselong (Cookie.getvalue ());
Date date = new Date (lastaccesstime);
Out.write (date.tolocalestring ());
}
}
} else {
Out.write ("This is your first time to visit this site!") ");
}
After the user has accessed, reset the user's access time, store it in a cookie, and then send it to the client browser
Cookie cookie = new Cookie ("LastAccessTime", System.currenttimemillis ()
+ "");//Create a Cookie,cookie name is LastAccessTime
The cookie object is added to the response object so that the server outputs the cookie to the client browser when it outputs the contents of the response object.
Response.addcookie (cookie);
}
Java EE Learning-cookie use example