session in jsp

Session in JSP explains you about how to use HttpSession in web application at the JSP page.

session in jsp

session in jsp

In this section you will learn about session in JSP. Here you will learn about how to use the HttpSession in JSP.

session in JSP is defined as an implicit object that implements the HttpSession interface in the core of its Servlet. Session specifies the single interactive user. An implicit object session, specifies the client specific conversation. The implicit object session is used for creating a session for a single user that specifies whether the user is active or inactive within the created session. Using the session object you can invoke various of its methods declared in the interface HttpSession.

Some of the commonly used methods that a session object can invoke are as follows :

  • getAttribute(String name) : This method is used to get the attribute value that is set using setAttribute() method within this session.
  • getAttributeNames() : This method is used to get all the attributes names that are set using setAttribute() method.
  • getCreationTime() : This method is used to get the time of the session when it was created.
  • getId() : This method is used to get the unique id of the session.
  • getLastAccessedTime() : This method is used to get the last accessing time of user with this session.
  • getMaxInactiveInterval() : This method is used to get the time in seconds that specifies the maximum time interval of user with this session that a Servlet container will opened for accessing.
  • setMaxInactiveInterval(int interval) : This method is used to set the time in seconds that specifies the maximum time interval of user with this session that a Servlet container will set for accessing.
  • setAttribute(String name, Object value) : This method is used to set the attribute value for the user within this session.
  • isNew() : This method is used for checking whether the specified session is new or not.
  • invalidate() : This method is used for ending this session.
  • removeAttribute(String name) : This method is used to remove the object attached with the specified attribute's name for this session.

Example

In this example we will describe you how to use the session object in the JSP page. This example explains you how the session object invokes the various of methods which performs an action. We will use the Eclipse IDE for compiling the JSP and Tomcat 7 for deploying the application. In this example we have used some of the methods discussed above.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Session In JSP</title>
</head>
<body>
<%
request.getSession(true);
out.println("Is Session New : "+session.isNew());
out.println("Session Creation Time : "+session.getCreationTime());
Object name = "Raju";
Object title = "Singh";
session.setAttribute("name", name);
session.setAttribute("title", title);
out.println("Name = "+ session.getAttribute("name"));
out.println("Title = "+ session.getAttribute("title"));
java.util.Enumeration e = session.getAttributeNames();
out.println("Attributes Are : ");
while(e.hasMoreElements())
{
out.println(e.nextElement());
}
session.invalidate();
%>
</body>
</html>

When you will compile and deploy the above example you will get the output as follows :

Download Source Code

For more detail you may follow the link http://www.roseindia.net/jsp/jspsession/