Servlets & Jsp-httpsession

Source: Internet
Author: User

For the contents of the Session, refer to the http-session mechanism

Create and retrieve HttpSession

The HttpSession instance can be obtained through the Httpservletrequest.getsession method. The HttpServletRequest has two overloaded getsession methods:

 Public HttpSession getsession ();  Public HttpSession getsession (boolean create);

The parameterless GetSession method returns the current HttpSession instance object and creates a new HttpSession instance if the HttpSession instance does not currently exist. If the HttpSession instance does not currently exist, the GetSession (false) method does not create a new HttpSession instance, and GetSession (true) is the same as getsession ().

Setting and Getting properties

The SetAttribute method can set property values on an HttpSession instance. The HttpSession property value can be not only a String type, but also a type that implements the java.io.Serializable arbitrarily. This allows the Servlet container to serialize the session to a file or database when the server is running out of memory. If the HttpSession property value is not serializable, an exception is thrown when the Servlet container attempts to serialize the session.

The GetAttribute method can get the property value of the HttpSession instance, and the Getattributenames method is to get the name of all the properties of the HttpSession instance.

Other methods

GetId: Returns a String that contains the unique identifier assigned to this session. Identifiers are assigned by the Servlet container and are implementation-dependent.

GetCreationTime: Returns the time that this session was created.

Getlastaccessedtime: Returns the time at which the client last sent a request associated with this session.

Getmaxinactiveinterval: Returns the idle time, in seconds, for this session.

Setmaxinactiveinterval: Sets the idle time, in seconds, for this session.

Invalidate: Invalidates this session, and then cancels the binding to any object bound to it.

IsNew: Determines whether this session is new.

Servlets & Jsp-httpsession

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.