Java Reference
In-Depth Information
The session Object
The implicit session object holds a reference to a javax.servlet.http.HttpSession object.
The HttpSession object is used to store objects between client requests. It provides an almost
state-full HTTP interactivity. The session object is initialized by a call to the
pageContext.getSession() method in the generated servlet. The code snippet that initializes
the session is as follows:
session = pageContext.getSession();
An example of using the implicit session object can be found in Listing 15.2.
L ISTING 15.2
UseSession.jsp
<%@ page errorPage=”errorpage.jsp” %>
<html>
<head>
<title>UseSession</title>
</head>
<body>
<%
// Try and get the current count from the session
Integer count = (Integer)session.getAttribute(“COUNT”);
// If COUNT is not found, create it and add it to the session
if ( count == null ) {
count = new Integer(1);
session.setAttribute(“COUNT”, count);
}
else {
count = new Integer(count.intValue() + 1);
session.setAttribute(“COUNT”, count);
}
out.println(“<b>Hello you have visited this site: “
+ count + “ times.</b>”);
%>
</body>
</html>
Search WWH ::




Custom Search