Pr.Pg Next Pg

HttpSession Object Methods tutorials

  • Below is the method available with HttpSession object:

 

S.N.

Method

Description

1

public Object getAttribute(String name)

 

This method returns the object bound with the specified name in this session, or null if no object is bound under the name.

2

public Enumeration getAttributeNames()

 

This method returns an Enumeration of String objects containing the names of all the objects bound to this session.

3

public long getCreationTime()

 

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

4

public String getId()

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

5

public long getLastAccessedTime()

 

This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.

6

public int getMaxInactiveInterval()

 

This method returns the maximum time interval, in seconds, that the web container will keep this session open between client accesses.

7

public void invalidate()

This method invalidates this session and unbinds any objects bound to it.

8

public Boolean isNew()

 

This method returns true if the client does not yet know about the session or if the client chooses not to join the session.

9

public void removeAttribute(String name)

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

10

public void setAttribute(String name, Object value) 

This method binds an object to this session, using the name specified.

11

public void setMaxInactiveInterval(int interval)

This method specifies the time, in seconds, between client requests before the web container will invalidate this session.

 


 

JSP program to describe how to use the Session object to find out the creation time and the last-accessed time for a session.

Sam Sir

<%-- Session.jsp--%>

<%-- JSP program to describe how to use the Session object to find out the creation time and the last-accessed time for a session. --%>

<%@page import="java.util.Date;" contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

String heading;

Integer accessCount =

(Integer)session.getAttribute("accessCount");

if (accessCount == null) {

accessCount = new Integer(0);

heading = "Welcome, Newcomer";

} else {

heading = "Welcome Back";

accessCount = new Integer(accessCount.intValue() + 1);

}

session.setAttribute("accessCount", accessCount);

out.println(

"<CENTER>\n" +

"<H1>" + heading + "</H1>\n" +

"<H2>Information on Your Session:</H2>\n" +

"<TABLE BORDER=1>\n" +

"<TR BGCOLOR=\"#FFAD00\">\n" +

"<TH>Info Type<TH>Value\n" +

"<TR>\n" +

"<TD>ID\n" +

"<TD>" + session.getId() + "\n" +

"<TR>\n" +

"<TD>Creation Time\n" +

"<TD>" +

new Date(session.getCreationTime()) + "\n" +

"<TR>\n" +

"<TD>Time of Last Access\n" +

"<TD>" +

new Date(session.getLastAccessedTime()) + "\n" +

"<TR>\n" +

"<TD>Number of Previous Accesses\n" +

"<TD>" + accessCount + "\n" +

"</TABLE>\n" +

"</CENTER>");

%>

</body>

</html>

Output

Session

 

session

Pr.Pg border                                              Next Pg