Session and Cookie

Source: Internet
Author: User

Session: Users open a browser, click multiple hyperlinks, access the server multiple Web resources, and then close the browser, the entire process is called a session.

The difference between a session and a cookie:

Cookies:
A cookie is a client-side technology in which a program writes each user's data to a 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. In this way, the Web resource handles the user's own data.

Session:
Session is a server-side technology, using this technology, the server at run time can be used for each user's browser to create a unique session object, because the session for the user browser exclusive, so the user access to the server's Web resources, Can put their own data in the session, when the user to access other Web resources on the server, the other Web resources from the user's own session to remove data for the user Service.

In Java, the cookie is encapsulated into the Javax.servlet.http:Cookie class. Each cookie is an object of the cookie class.

cookies have non-cross-domain names .

Cookie Expiration : The maxage of the cookie determines the validity period of the cookie. Getmaxage () and Setmaxage () Read and write MaxAge properties. MaxAge is positive, it means that it disappears automatically after maxage seconds. MaxAge is negative, closing the browser window is invalid. The default maxage of -1,maxage is 0 to delete the cookie.

cookie modification, deletion : Delete a cookie, just create a new cookie with the same name. and set MaxAge to 0. and add to response to overwrite the original cookie.

Session life cycle : The server updates the session's last access time and maintains the session.

duration of Session :

Common methods of Session :

Setmaxinactiveinterval (int interval)//Set the effective time, note the unit in seconds.
Getmaxinactiveinterval (int interval)//Get effective time
SetAttribute (String name, object value)://Adds an object to the session scope with a specified name
GetAttribute (String name)//Gets the object in scope by making a name
RemoveAttribute (String name)//Remove an object from the scope by making a name
Getattributenames ()//Get all the objects in scope

Session and 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.