Javaweb Basics-Session Management Cookie

Source: Internet
Author: User

First, what is a conversation

  Open the browser, browse various resources, click on various hyperlinks, until close the browser, the whole process is called the session

Two kinds of techniques of session management

1.Cookie

    Based on the client. Written to the user's browser in the form of a cookie. When users use a browser to access Web resources on the server, they take their own data.

2.Session

    Based on the service side. The session is similar to the client's account on the server side. Use map to store. The server can create an exclusive session object for each user's browser at run time.

Third, the use of cookies 

  Use of cookies:
Server used to track client status
Save Shopping Cart, etc.
Displays the name of the last login

(used to save the shopping cart, automatic landing)

Iv. Common Cookie API

   in the Javax.servlet.http.Cookie This package, you can query to a few common APIs

Cookie (string name, String value): The constructor of the cookie

get/setMaxAge (int age): The maximum lifetime of the operating cookie (the expiration date of the cookie), and the closing of the browser when the validity period is set will save the cookie to the hard disk. Without setting the browser session is closed and the cookie does not exist. <0 is the lifetime in memory . the unit is in seconds . >0 to set an expiration time , =0 is killed.

GetName (): Get the name of the cookie

get/Setvalue (String newvalue): The value of the manipulation cookie

get/Setpath (): A valid path for manipulating cookies, such as setting the valid path of a cookie to "/XDP", when a browser accesses a Web resource in the "XDP" directory, it will bring a cookie

V. Operating cookies in Javaweb

Convenient Way (Proficient): Response.addcookie (): Server to the client (browser) to save cookies service-side send-out (add)
Request.getcookies (): Add S, return the browser returned by the cookie (array) client get (GET)

For example, get the last login time: 

   PrintWriter out = Response.getwriter ();        Gets the Cookie array passed when the browser accesses the access server cookie[] cookies = request.getcookies ();             If the user is a first-time access, then the resulting cookie will be null if (Cookies!=null) {out.write ("The time you last visited is:");                 for (int i = 0; i < cookies.length; i++) {Cookie cookie = 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!")        "); }//user reset the user's access time after access, stored in a cookie, and then sent to the client browser cookie cookie = new Cookie ("LastAccessTime", System . Currenttimemillis () + "");//Create a Cookie,cookie name is LastAccessTime//Add the cookie object to the Response object,    When the server outputs the contents of the response object, it also outputs the cookie to the client browser Response.addcookie (cookie); }

Javaweb Basics-Session Management Cookies

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.