• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session data

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rama and Namita,
I haven't had the chance to try what you suggested, but I would like to say Thanks.
Steve
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic