Java EE Learning-cookie use example

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.