Use of the Java session cookie

Source: Internet
Author: User

Cookies; Session; URL rewriting;

Cookies are used in the Java EE project, and the cookie is encapsulated as a Java.servlet.http.Cookie class. Each cookie is an object of the cookie class. The server operates on the client cookie by manipulating the cookie class object. Get all cookies submitted by the client (in cookie[] array) via Request.getcookies (), via Response.addcookie (Cookiecookie) Set a cookie to the client. The cookie object saves the user state in the form of a Key-value property pair, a cookie object that holds a property pair, and a request or response uses multiple cookies at the same time. The non-cross-domain name of the cookie and the expiration date of the cookie.

Server-side Get cookiecookie[]cookies = Request.getcookies (); for (int i = 0; Cookies! = null && i < cookies.length;i++ ) {         Cookiecookie = cookies[i];         if ("username". Equals (Cookie.getname ())) {         }      }

Server-side Settings cookie

String username = "sa";

Cookie Usernamecookie = new Cookie ("username", username);

Response.addcookie (Usernamecookie);

Session is a server-side use of the mechanism to record client status, the use of more than a cookie is simpler, the corresponding increase the storage pressure of the server. The session is another mechanism for recording client status, but the cookie is stored in the client browser and the session is stored on the server. When the client browser accesses the server, the server logs the client information to the server in some way. This is the session. When the client browser accesses it again, it only needs to query the client's status from that session. If the cookie mechanism is to determine the customer's identity by checking the "pass" on the client, then the session mechanism verifies the customer's identity by checking the "customer schedule" on the server. Session is equivalent to a program on the server set up a customer profile, when customers visit only need to query the customer file table on it. Session is saved on the server side. In order to obtain higher access speed, the server usually puts the session in memory. Each user will have a separate session. If the session content is too complex, a large number of clients accessing the server can cause memory overflow. Therefore, the information in the session should be as concise as possible. Session is created automatically when the user accesses the server for the first time. It should be noted that only access to the JSP, servlet and other programs will be created session, only the HTML, image and other static resources do not create session. If the session has not been generated, you can also use Request.getsession (true) to force the session to be generated. After the session is generated, the server updates the last access time of the session and maintains the session as long as the user continues to access it. Each time a user accesses a server, the server considers the user's session "active" once, regardless of whether the session is read or written.

  server-side get session    Httpsessionsession = Request.getsession ();    Stringusername = (String) session.getattribute ("username");//server-side settings session    Httpsessionsession = Request.getsession ();    Session.setattribute ("username", "sa");

Use of the Java session cookie

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.