Cookies and session

Source: Internet
Author: User
Tags set cookie


1, the technology of saving HTTP status information on the client

2, the server read the cookie is issued from the browser, he saved in the request header heavy

3. Access to Cookies

Cookie Cookie=new Cookie ("name", "value");

4/Common methods

Cookies. Setmaxage (valid time value)//If no valid time is set, the cookie is not written to the client hard disk and the cookie is destroyed when the browser is closed

SetName (), SetValue (), GetName (), GetValue ();

Response.addcookie (cookie); //Add a cookie in response

Request GetCookies ()//Returns an array of cookie types, and then uses GetName (), GetValue () to visit his name, and value


1. Session Life cycle

1. Created when the browser accesses the server,

2. The session closes when the browser is closed

3, the session does not set the validity period, all browser closed session revocation

Note: Therefore, you need to set the validity period in the session in the e-commerce servlet

2. Creation of Session

HttpSession session=request.getsession ()

Request.getsession (FALSE); //Read session only and not create

3. Session usage is cookie-based

There will be a session ID when the session is created.

The ID is returned to the browser in the form of a cookie, which is brought to the Servlet,servlet by the ID to receive the session in response to the browser's other actions.

The ID is returned to the browser in the form of a cookie, but the validity period is not set

HttpSession session=request.getsession ();

String Sessionid=session.getid ();

Cookie Cookie=new Cookie ("Jsessionid", SessionID)//jsessionid The default cookie value, which is overridden in order to reach the purpose of overwriting

Cookie.setpath ("/project directory")//

Cookie.setmaxage (30*60); //Set cookie expiration date

Response.addcookie (cookie)//Return to Browser cookie

Session.setattribute ("name", "value"); //Set the value of session

4. Closing of Session

Default Time to

Manual forced Shutdown----session.invalidate ()

Server is not properly shut down

The server shuts down normally----session passivation (Session Resurrection)

Disable in the cookie, can I use the session?

Yes, can be bound to the Address bar recodeurl (String URL);

This article is from the "design mode" blog, so be sure to keep this source

Cookies and session

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: 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.