• Post Reply Bookmark Topic Watch Topic
  • New Topic

question on session

 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we place an arraylist into session?
i know how to place the string in session.in one interview they have asked this question
 
Ola Daniel
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer: YES... an ArrayList can be placed in session.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpSession.setAttribute's second argument type is java.lang.Object. Therefore, you can bind anything to session.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
HttpSession.setAttribute's second argument type is java.lang.Object. Therefore, you can bind anything to session.


If you are using Java 5 you can also bind primitives becasue of the auto boxing feature.
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please send me the samplecode for placing an arraylist into session.
i have tried but iam getting classcastexception
ArrayList a= new ArrayList();
a.put("aa");

HttpSession.setAtrtribute("Arraylist","a");
please reply to this
 
Darren Edwards
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you have posted will not cause an exception (nor will it run as HttpSession is not a variable, it's a class).


[ June 20, 2006: Message edited by: Darren Edwards ]
 
Kavitha Govindaraj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi



try this..


Syntax:
void setAttribute(java.lang.String name, java.lang.Object value)
Binds an object to this session, using the name specified.

A simple search in Servlet API will solve your problem. So do some basic work before asking help from anyone. Sorry to be too hard. But that will really help you.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can place any Object in a Session but I truly recommend to store only java.io.Serializable objects.
 
smith stevenson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx i was looking for the answer as well.
 
trinadh reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when iam retriving using getAttribute iam getting null
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by trinadh reddy:
when iam retriving using getAttribute iam getting null


Why would you post such a short statement to the end of another thread?

If reading the discussion in this one didn't solve your problem, start a new one with a a well written question.
By well written, I mean, explain what you've done.
Use code examples if necessary (be sure to use UBB code tags if you post code).

In general, if you take some time and show some effort you stand a much better chance of getting a useful answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!