Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Friday, March 20, 2009

JSP Session Variables

Note that the codes in this blog post have not been tested. I will update this post once I have tried implementing them.

Session Variables
Session variables are useful for associating data with your visitor. The variables will disappear once the session expires. Here is what you need:
1) A HTML Form to retrieve data from the visitor

<FORM METHOD=POST ACTION="savedata.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<INPUT TYPE=SUBMIT>

2) savedata.jsp – This is the target page of the form, and this page manipulates the data.

<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>

<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
</BODY>
</HTML>

First, you retrieve the value of the parameter "username" that was posted using the form; then you set the attribute "theName" with the value that was retrieved. To retrieve the session value, use the getAttribute(..) function and pass in the attribute name.

Source: http://www.jsptut.com/Sessions.jsp.