Servlet Session processing, servletsession

Source: Internet
Author: User

Servlet Session processing, servletsession

Several important methods available in the HttpSession object:

 

1 public ObjectGetAttribute(String name)

This method returns an object with the specified name in the session. If no object with the specified name is returned, null is returned.

2 public EnumerationGetAttributeNames()

This method returns the String object enumeration. The String object contains the names of all objects bound to the session.

3 public longGetCreationTime()

This method returns the time when the session was created, in milliseconds, from midnight, January 1, January 1, 1970, GMT.

4 public StringGetId()

This method returns a string containing the unique identifier assigned to the session.

5 public longGetLastAccessedTime()

This method returns the last time the client sends the session-related request from midnight, January 1, January 1, 1970, in milliseconds.

6 public intGetMaxInactiveInterval()

This method returns the maximum time interval for the Servlet container to enable session opening during client access, in seconds.

7. public voidInvalidate()

This method indicates that the session is invalid and unbound from any objects on it.

8 public booleanIsNew(

If the client does not know the session, or the client chooses not to input the session, this method returns true.

9 public voidRemoveAttribute(String name)

This method removes the object with the specified name from the session.

10 public voidSetAttribute(String name, Object value)

This method binds an object to the session with the specified name.

11 public voidSetMaxInactiveInterval(Int interval)

This method specifies the time between client requests before the Servlet container instructs that the session is invalid, in seconds.

 

The default Session timeout time in Tomcat is 20 minutes. You can configure it in web. xml.

<Session-config> <session-timeout> 60 </session-timeout> <! -- In minutes --> </session-config>
 

When you use request. getSession (booleancreate) to obtain a Session, if the client's Session does not exist, a Session is created and then returned.

Note: The Session is created only when you access Servlet, JSP, and other programs. It is not created only when you access static resources such as HTML.

Sessions can also store java bea and any java classes and objects.

URL address Rewriting

URL address rewriting is a solution that does not support cookies on the client.

The principle of URL address rewriting is to overwrite the Session id information of the user to the URL address. The server can parse the rewritten URL to obtain the Session id, even if the client does not support cookies, you can also use Session to record user statuses.

The HttpServletResponse class provides encodeURL (Stringurl) for URL address rewriting.

<A href = "<% = response. endodeURL (" index. jsp? Wd = java ") %>">

This method will automatically determine whether the client supports cookies. If not, the user Session id will be rewritten to the URL. The output after rewriting is similar

<A href = "<% = response. endodeURL (" index. jsp; Jsessionid = 0CCD096E7F8A123E345EF77? Wd = java ") %>">


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.