• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a Session Attribute in another Servlet  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP that has a select list. Is there a way that the servlet that is connected to this JSP store the list contents as a session attribute to be used in another servlet?

The first servlet takes the contents of the list and commits the data to a data file.

The second servlet displays this data in an online report. I would like to use the list data without having to make a connection and retrieve the data since the session should have the data already.

Please help me figure out how to store the list to an attribute then use this session attribute in another session.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what session scope is for.
Look at the setAttribute and getAttribute method of HttpSession.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use the set and get session attributes all the time. Except this time it not just a string value it is an array.

My set code:


What will my servlet code code need to be to get or use the session attribute?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except this time it not just a string value it is an array.
So? Why is that so different? Do you know how to cast in Java?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second argument in setAttribute and the return type of getAttribute is java.lang.Object so you can put anything in there. You just (as Bear metioned) need to cast it down to its original type when you retrieve it.
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do I set the left side of my argument to be? This is what I came up with but it will not work. Can you give me some more guidance?

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Dyke:
What do I set the left side of my argument to be? This is what I came up with but it will not work. Can you give me some more guidance?



Why are you nesting these?

It's very simple, bind the array to session scope from your first servlet:


Then from any servlet or JSP that needs to use the array:
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It was this part I did not know and understand:

(String[])session.getAttribute("ssPickListArray");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!