This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Servlets and the fly likes question on session Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "question on session" Watch "question on session" New topic
Author

question on session

pvsr rao
Ranch Hand

Joined: Oct 05, 2005
Posts: 102
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

Joined: Jul 27, 2005
Posts: 105
Short answer: YES... an ArrayList can be placed in session.


SCJP 1.4, SCWCD 1.4
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HttpSession.setAttribute's second argument type is java.lang.Object. Therefore, you can bind anything to session.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

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.


Groovy
pvsr rao
Ranch Hand

Joined: Oct 05, 2005
Posts: 102
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

Joined: Aug 17, 2005
Posts: 69
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

Joined: May 27, 2004
Posts: 28
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.


Thanks <br />Kavitha G <br />SCJP
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
You can place any Object in a Session but I truly recommend to store only java.io.Serializable objects.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
smith stevenson
Greenhorn

Joined: Jun 20, 2006
Posts: 1
Thanx i was looking for the answer as well.
trinadh reddy
Ranch Hand

Joined: Sep 22, 2005
Posts: 58
when iam retriving using getAttribute iam getting null
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: question on session
 
Similar Threads
Struts - maintaing form fields data
How to use httprequest object through out the application
serialization question deleted for a reason?
What are Spool and Expiry threads?
sending Vector Object from servlet to JSP page