File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Session data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session data" Watch "Session data" New topic
Author

Session data

Steven Lyner
Greenhorn

Joined: Jul 02, 2001
Posts: 10
I am trying to store session data and I am having a problem. I created a repeating INPUT field in a JSP page document and I would like to pass the data from page to page as session data. I would appreciate it if someone can tell me how to do this.
Thanks,
Steve
Rama Raghavan
Ranch Hand

Joined: Aug 22, 2001
Posts: 116
Obtain a handle to session object
HttpSession session = request.getSession();
If a session id does not exist, the api will create on for you.
Then store an object using
session.setAttribute("name", object);
You can then retrieve using
(class class)session.getAttribute("name")
Make sure you cast the retrieved object to the expected type.
Hope this helps -
------------------
_________________________
Rama Raghavan
SCJP2


Rama Raghavan<br />SCJP2<br />SCWCD
namita pa
Ranch Hand

Joined: Sep 06, 2001
Posts: 56
hi
here i would like to add that if you make any changes to that particular variable don't forget to add it again to the session with the same name.if you would not do this then when you try to retrieve the variable in the next page it would not show the updated value
regards
namita


------------------
Steven Lyner
Greenhorn

Joined: Jul 02, 2001
Posts: 10
Rama and Namita,
Thanks. I tried to do what you said but, unfortunately I had some problems. When I added the code HttpSession session = request.getSession();, I got a compiler message that said that a variable called session was already defined. Maybe I didn't explain my problem properly. One JSP contains a table of "myfield" that occurs 30 times. I would like to add these fields to my session in a JSP page that I call from the first page. When I try to add a single field to my session it works fine using "session.setAttribute("sessionField", request.getParameter("fieldfromfirstJSP"));" I also didn't understand what you meant by "(class class)".
Thanks,
Steve
namita pa
Ranch Hand

Joined: Sep 06, 2001
Posts: 56

compiler message that a variable called session was already defined. is quite genuine as there is an inbuilt variable session for jsp .
HttpSession session = request.getSession();
is defined for servlets
In jsp page you write
<%@ page session="true"%> directly
Now try and that error should not come
dont confuse with(class class)
If your attribute userid which you are saving in session is a int object then
int i=Integer.parseInt((String)session.getAttribute("UserId"));
this is what it meant
hope its clear now
all the best

------------------
Rama Raghavan
Ranch Hand

Joined: Aug 22, 2001
Posts: 116
Oops that was a typo (class class).
I meant to say (cast to expected class). You need to cast the retrieved object to the expected type of the data stored in the session.
Also - I gave a verbose description and is valid if you are trying to access session in a servlet.
In case of a JSP, just add a page directive session=true as mentioned in an earlier response. This automatically gives you handle to a session.
From here - just use session.setAttribute("sessionField", value)
As you see it is a name-value pair being stored in the session.
If you try to setAttribute with the same name, the old value will get replaced.
Hope this helps -
------------------
_________________________
Rama Raghavan
SCJP2
Steven Lyner
Greenhorn

Joined: Jul 02, 2001
Posts: 10
Rama and Namita,
I haven't had the chance to try what you suggested, but I would like to say Thanks.
Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session data