Cookie for Java Web

Source: Internet
Author: User

First, the cookie is the server side to write the identifier to the browser, the browser access server automatically carry

Server-side Cookie creation

Random random = new random (); Cookie cookie = new Cookie ("Writecookie", "Writecookie" +random.nextint); Cookie.setmaxage (60);//Set Browser cookie lifetime , in Seconds//cookie.setpath (Request.getcontextpath ()); Cookie.setpath ("/");//Set the scope of the cookie to the entire site Response.addcookie ( Cookies);

Server gets the cookie of the browser

cookie[] cookies = request.getcookies (), if (Cookies!=null) {for (Cookie cookie:cookies) {out.println (Cookie.getname () + ":" +cookie.getvalue ()); Out.println ("<br>");}}

Second, the application of the cookie scenario

1. Automatic User Login

2, browse the latest products

This article is from the "11137669" blog, please be sure to keep this source http://11147669.blog.51cto.com/11137669/1905844

Cookie for Java Web

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.