JSP Implicit object session with example - Java @ Desk

Wednesday, May 7, 2014

JSP Implicit object session with example

JSP Implicit object session with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example

JSP session object is a type of javax.servlet.http.HttpSession class. Session object represents the data associated with the user for the complete session. If any attribute is set using the session object, it will be available to the user for that particular session unless the browser is not closed or session is not invalidated.

Whenever a user request for a particular JSP for the first time, the container automatically creates a session and holds the session attributes till it is active. This session object will be shared across pages visited by the user.

Session creation is an heavy operation. By default the session creation for the JSP page is true. Can we disable session creation for a JSP page? Click here to know

Following are the methods of a session :
1) setAttribute(String, object)
2) getAttribute(String name)
3) isNew() - The isNew() method of session object returns a true value if the session is new. If the session is not new, then a false value is returned.
4) invalidate() - It kills the session associated with the user.

Example :

<%@ 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>
 Session Timeout :
 <%=session.getMaxInactiveInterval()%>

 <%
  session.setAttribute("loanInterestRate", 10);
 %>
</body>
</html>






No comments:

Post a Comment