Académique Documents
Professionnel Documents
Culture Documents
http://www.jsptutorial.net/jsp-session.aspx
RSS FEED
Search
Search Keywords...
Ads by Google
Tutorial
Learning C# Tutorial
JSP
JSP Tutorial Servlet Tutorial JSP Standard Tag Library JSP Cheat Sheet JSP FAQs JSP Resources
public void setAttribute(String name, Object value) throws IllegalStateException public Object getAttribute(String name) throws IllegalStateException public void removeAttribute(String name) throws IllegalStateException
Let's take a look at an example how to use session object. In this example, we have three pages: In the first page we collection data from user, after that user submits the form to a second page which is used to store data in session. In the last page, we get data back from the session and display it.
<html> <head> <title>JSP Form</title> </head> <body> <form method="post" action="savetosession.jsp"> <table> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td>
1 of 3
7/27/2011 11:14 PM
http://www.jsptutorial.net/jsp-session.aspx
</tr> <tr> <td>Last Name</td> <td><input type="text" name="lastName" /></td> </tr> <tr> <td>Comments</td> <td><textarea name="comments" cols="30" rows="5"></textarea></td> </tr> <tr> <td colspan="2"><input type="submit" value="submit" /></td> </tr> </table> </form> </body> </html>
Copyright by www.jsptutorial.net 2009 - 2010. All Rights Reserved. JSP Tutorial | Java Swing | Privacy Policy
In the form above, when user enters information, clicks submit button, the data goes through the page savetosession.jsp. In the savetosession.jsp page we save all the submitted data into the session object and forward the request to another page called display.jsp page
<%@page contentType="text/html" pageEncoding="UTF-8"%> <% String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String comments = request.getParameter("comments"); // save data into the session session.setAttribute("firstName", firstName); session.setAttribute("lastName", lastName); session.setAttribute("comments", comments); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <%-- forward to the display.jsp page--%> <jsp:forward page="display.jsp" /> </body> </html>
As you see the code above, we use the setAttribute() method to save data into the session object. Here is the page for displaying data in the session object: display.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>Displaying data in Session</title> </head> <body> <h1>Displaying data in session object</h1> <table> <tr> <td>First Name</td> <td><%= session.getAttribute("firstName")%></td> </tr> <tr>
2 of 3
7/27/2011 11:14 PM
http://www.jsptutorial.net/jsp-session.aspx
<td>Last Name</td> <td><%= session.getAttribute("lastName")%></td> </tr> <tr> <td>Comments</td> <td><td><%= session.getAttribute("comments")%></td> </tr> </table> </body> </html>
The code is obvious, we used the getAttribute() method of the session object to retrieve data which was entered in the form and displayed it on the page by using expression.
Delicious
Digg
StumbleUpon
Previous page
Next page
3 of 3
7/27/2011 11:14 PM