Cookie implementation records last logon time and delete cookies

Source: Internet
Author: User
Tags send cookies

Below we implement a cookie that gets the client and then determines whether it is the last time the login was made.
If so, we turn it into a time format output.
We're going to get the cookie first and then add the cookie because we didn't have the last login time for the first login.

public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti
         On {response.setcontenttype ("text/html;charset=utf-8");
         PrintWriter out = Response.getwriter ();
         Get the client all the cookies cookie[] cookies=request.getcookies (); To find the cookie that stores the last logon time from the cookie//cookie is a key-value pair, and the keys and values are both string's if (Cookies!=null) {for (int i=0;i& lt;cookies.length;i++) {String name = Cookies[i].getname ();//Gets the cookie's name if ("Lastacces
                     Stime ". Equals (name)) {String time = Cookies[i].getvalue ();//Get the last logon time, which is a string type
                     Long T = Long.parselong (time);
                 Out.println ("Last Logon time is:" +new simpledateformat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date (t)); }} out.println ("<a href= '" +request.getcontextpath () +/servlet/delcookie ' > Delete cookie&

Lt;/a> ");         Send this logon time as a cookie to the client//create Cookie cookie = new Cookie ("LastAccessTime", System.curre
         Nttimemillis () + ""); Cookie.setmaxage (60*15);//cookie is stored on disk Cookie.setpath (Request.getcontextpath ());///day35_cookie as long as the path to/day35_ Cookies//cookie.setpath ("/") can be received at the beginning of the cookie;///day35_cookie//Send cookies to client Response.addcooki
         E (cookie); * * Cookies are stored by default in browser memory, once the browser is turned off, cookies are all gone * setmaxage (time) to set the maximum lifetime of the cookie, the default unit is seconds * SETMAXAG 
          E (-1); default is-1, once the browser is off, all cookies are gone * setmaxage (0): Browser does not close, cookies are gone * setmaxage (positive number) *
          * What conditions the client will send cookies to the server side. * The path to the resource being accessed must match * http://localhost:8080/day35_cookie/servlet/cookie1 * http://localhost:8080/day35_ Cookie/servlet/cookie2 */}

Above we also introduced the cookie Setmaxage () method, this is to set the cookie's survival time.
Of course, we can also manually delete cookies, above we provide a method,
Above our jump to the Delete cookie interface, delete cookies.
So how do we delete cookies, and we'll take a look at the servlet that deletes cookies.

    Response.setcontenttype ("Text/html;charset=utf-8");

        Delete Cookie cookie
        cook = new Cookie ("LastAccessTime", "");
        Cook.setmaxage (0);
        Set path, or you may mistakenly delete
        Cook.setpath (Request.getcontextpath ());
        Response.addcookie (Cook);       

Let's create a new cookie with the same name as the cookie that needs to be deleted, overwrite it, and then set the survival time to 0.
Note that we need to set the path of the new cookie, otherwise there may be a cookie with the same name under another path that cannot be deleted.

For path problems, we can set the path of the cookie ourselves, and cookies under the same path can be used together under different servlet.

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.