Introduction to sessions in the servlet and examples of usage

Source: Internet
Author: User

In the servlet, the session is encapsulated in the Javax.servlet.http.HttpSession interface, which is built on the basis of a cookie or URL rewrite to get an httpsession instance, You can use the HttpServletRequest getsession () method to obtain

HttpServletRequest has two overloaded getsession () methods, one that takes a Boolean type, the other with no arguments, and the GetSession () method and the GetSession (true) method function, That is, if the corresponding client has already generated a session, it will return the old one, otherwise this method will produce a session ID and bind to the corresponding client, if GetSession (false) Indicates that if the corresponding client already has a corresponding session, then the old session is returned, otherwise no new session will be generated. You can use the Isnow () method on the HttpSession object to determine whether this session is a new

HttpSession Common methods

public void setattribute (String name,object value)

Bind a value object to a session with a name name

public Object GetAttribute (String name)

Gets the property value of name and returns NULL if the property does not exist

public void RemoveAttribute (String name)

Removes the Name property from the session and does not throw an error if it does not exist.

Public enumeration Getattributenames ()

Returns the and session-related enumeration values

public void Invalidate ()

Invalidates the session while deleting the Property object

Public Boolean isnew ()

Used to detect whether the current customer is a new session

Public long GetCreationTime ()

return session creation Time

Public long Getlastaccessedtime ()

Returns the time that the Web container received the last request from the customer during session time

public int Getmaxinactiveinterval ()

Returns the maximum time that a customer requests during a session is seconds

public void Setmaxinactiveinterval (int seconds)

Maximum time allowed for customer requests

ServletContext Getservletcontext ()

Returns the context of the current session in which the ServletContext object enables the servlet to communicate with the Web container

Public String getId ()

Returns the identification number during a session

A simple example of saving information to session

Sessionlogin.html

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.