Thursday, 13 November 2014

jsp session implicit object

session is an implicit object of type HttpSession.
The session object is used to track session between requests sent by a single user.
it can also be used to get,set and remove attributes in session scope.

Popular method of implicit object session(description as per javadoc)

void setAttribute(String name,Object value)
Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.

Object getAttribute(String name)
Returns the object bound with the specified name in this session, or null if no object is bound under the name.

void setMaxInactiveInterval(int interval)
Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.

long getCreationTime()
Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.

String getId()
Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.

ServletContext getServletContext()
Returns the ServletContext to which this session belongs.

Enumeration getAttributeNames()
Returns an Enumeration of String objects containing the names of all the objects bound to this session.

void removeAttribute(String name)
Removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing.

void invalidate()
Invalidates this session then unbinds any objects bound to it.

boolean isNew()
Returns true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request.

SessionDemo.jsp
<%@ 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>Insert title here</title>
</head>
<body>
<form action="welcomePage.jsp">
<input type="text" name="name">
<input type="submit" value="Enter!!">
</form>
</body>
</html>
welcomePage.jsp
<%@ 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>Welcome Page</title>
</head>
<body>
<%
String uname=request.getParameter("name");
out.print("Hiiii "+ uname);
session.setAttribute("user",uname);
%>
<a href="other.jsp">Visit Another Page </a>
</body>
</html>
other.jsp
<%@ 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>Other Page</title>
</head>
<body>
<%
String name=(String)session.getAttribute("user");
%>
The Entered Name is---> <%=name %>
</body>
</html>
Output