This demo is in two parts. The first part (which I did not show in class), simply demonstrates that sessions work with javabeans.
There is a variable called session which is automatically created and you can use this to store information about the session.
Modify the files as follows. Note that you do not need to make any changes to the Bean code.
sessionDemo.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Session Demo</title>
</head>
<body>
<h2>Session Demo</h2>
<p>
<form method="GET" action="sessionDemo.jsp">
What is your name?
<input type="text" name="myname">
<p>
Enter a number:
<input type="text" name="num">
<p>
<input type="submit">
</form>
</body>
</html>
</body>
</html>
This simply adds one more field, called myname
sessionDemo.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:useBean id="sessionDemo" class="sessiondemo.SessionBean" scope="session" />
<html>
<head>
<title>Session Demo jsp</title>
</head>
<body>
<big>
<h1>Session Demo jsp</h1>
<jsp:setProperty name="sessionDemo" property="num" param="num" />
<p>
The sum is now <jsp:getProperty name="sessionDemo" property="sum" />
<p>
<%
String s = request.getParameter("myname");
session.setAttribute("Id",s);
if (request.getParameter("num").equals("0"))
session.invalidate();
%>
<form method="GET" action="sessionDemo2.jsp">
Enter a number (or 0 to end session):
<input type="text" name="num">
<p>
<input type="submit">
</form>
</big>
</body>
</html>
This adds a scriptlet, which accesses the object session which is of type HttpSession. This object is automatically created. One of its members is setAttribute, which we use here to store the user's name.
sessionDemo2.jsp
<jsp:useBean id="sessionDemo" class="sessiondemo.SessionBean" scope="session" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Session Demo Two</title>
</head>
<body>
<big>
<h1>Session Demo Two</h1>
<jsp:setProperty name="sessionDemo" property="num" param="num" />
<p>
<% out.println("Hello " + session.getAttribute("Id")); %>
<p>
The sum is now <jsp:getProperty name="sessionDemo" property="sum" />
<p>
<% if (request.getParameter("num").equals("0"))
session.invalidate();
%>
<form method="GET" action="sessionDemo3.jsp">
Enter a number (or 0 to end session):
<input type="text" name="num">
<p>
<input type="submit">
</form>
</big>
</body>
</html>
This uses the invalidate method of session, which terminates the session. It also uses the getAttribute method to get the name of the user, which was stored in the session object.
SessionDemo3.jsp is essentially identical to this.
<jsp:useBean id="sessionDemo" class="sessiondemo.SessionBean" scope="session" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Session Demo Three</title>
</head>
<body>
<big>
<h1>Session Demo Three</h1>
<jsp:setProperty name="sessionDemo" property="num" param="num" />
<p>
<% out.println("Hello " + session.getAttribute("Id")); %>
<p>
The sum is now <jsp:getProperty name="sessionDemo" property="sum" />
<p>
<% if (request.getParameter("num").equals("0"))
session.invalidate();
%>
<form method="GET" action="sessionDemo3.jsp">
Enter a number (or 0 to end session):
<input type="text" name="num">
<p>
<input type="submit">
</form>
</big>
</body>
</html>