CSCI.4220 Network Program, Fall 2006, JSP Session Demo

This demo is in two parts. The first part (which I did not show in class), simply demonstrates that sessions work with javabeans.

  1. Create a directory called sessiondemo in tomcat/webapps
  2. Copy these files to this directory. Note that if you click on these, your browser will run them, so you will need to view the source and save it.
    sessionDemo.html
    sessionDemo.jsp
    sessionDemo2.jsp
    sessionDemo3.jsp
  3. Create a subdirectory in sessiondemo called WEB-INF, create a subdirectory in WEB-INF called classes, and create a subdirectory in classes called sessiondemo
  4. Copy this file to tomcatwebapps/sessiondemo/WEB-INF/classes/sessiondemo sessionBean.java
  5. Compile this file with javac.
  6. Start up tomcat (if it is already running, you will have to restart it)
  7. In Internet Explorer, go to http://localhost:8080/sessiondemo/sessionDemo.html You are asked to enter a numeric value, do so (Note: there is no error handling in this simple code, so you can break it if you don't enter a value or enter a non-numeric value)
  8. Each time that you hit submit, a new window appears. However, the bean keeps track of the sum of the numbers that you have entered so far.
  9. Start up another instance of IE, and do the same thing. This starts a new session so the addition starts from zero. Note that you can maintain two simultaneous connections, each with its own sum.
There are only two things that are different between this application and the CurrencyConverter application.

Part 2: Accessing the session variable

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>