Simple Session

This is a simple JSP Program I had lying around to get accustomed to using the jsp language and the Java Reference Implementation.

Basic Concepts

enterName.jsp: The code is commented so it is self-explanatory... ( I hope ? )

<!-- enterName.jsp: example of session variable using Scriplets & HTML mixed -->

<%@page contentType="text/html"%>
<html>
<head><title>Session Example: enterName.jsp</title></head>
<body>

<!--
   1) Get the session id and display it to the user
   2) Enter a value in the text field and we will save it as a session variable
-->


<%-- THIS IS ONE WAY TO WRITE THE CODE, MIXING SCRIPLETS WITH HTML --%>
This is the session id <STRONG><%= session.getId()%></STRONG>
<BR>
 

<!-- check to see if the session in new, if not display session information -->
<% if ( session.isNew() ) { %>
    This is a new session ! <BR>
<%} else {
    // get the username parameter an save it!, by calling the function below,

    // the the form submit request parameter, named "USERNAME" is being save

    // as a session variable.. see displaySavedName.jsp for retrieval.
    session.setAttribute("USERNAME",request.getParameter("USERNAME"));
%>
    This is an existing session; created

    <%=new java.util.Date(session.getCreationTime())%> <BR>

 

    <!-- since it is not new provide a link to view the saved variable -->
    <A href='displaySavedName.jsp'>Show Saved Name</A>
<%}%>
 

<!-- basic HTML stuff, but notice that we call the jsp page on submit -->
<FORM method=POST action='enterName.jsp'>
Enter the name to associate to this session
<INPUT type='TEXT' name='USERNAME' size='20'> <BR/>
<INPUT type='SUBMIT' value='save name as session variable'>
</FORM>

</body>
</html>
 

Quickly a comment on the request object and how I used it.

Any variables contained in the HTML Form tag and/or passed on the URL are contained in the request object. This is a useful way to pass information around in HTML. The values are stored as named value pairs and can be retrieved  public Object getParameter( String objectName ).

Here is displaySavedName.jsp. This page shows how simple it is to retrieve variables for the session.

<!-- displaySavedName.jsp: example of retrieving session variables  -->

<%@page contentType="text/html"%>
<html>
<head><title>Session Example: displaySavedName.jsp</title></head>
<body>

<!-- make java sdk call to get value from the session -->
The saved name is <STRONG><%= session.getAttribute("USERNAME")%></STRONG>
<BR/>
 

<!-- provide link to return to starting page -->

<A href='enterName.jsp'>Go Back</A>
</body>
</html>
 

The name value saved with the session will continue to be saved until the session times out. The timeout value can be set using the method public void setMaxInactiveInterval(int interval).

Click here to see source code of enterName.jsp using just Scriplets. I find that sometime combines the HTML embedded in the Scriplets makes the code unreadable and also difficult to maintain. In the end the developer will have the final say, but the poor person left to maintain the code will be the true judge of the results.