Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes Array in session with JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Array in session with JSP" Watch "Array in session with JSP" New topic

Array in session with JSP

parvathi Viswanathan

Joined: Sep 25, 2000
Posts: 2
How can you store a String array in a session in JSP and access it from a different JSP page?
say, arr is a two dimensional array of Strings. I have put this in a session using session.setAttribute("session_array",arr).
In a different JSP file when I try to get this using the following.
String new_arry[][] = new String[rows][cols];
new_array = session.getAttribute("session_array");
I get an error saying new_array is a of type java.lang.String whereas the right hand side is an Object (java.lang.Object). Please help.
I have tried to declare this new_array as Object. But how do I use it as an array? What kind of casting / type conversion should I use?
Any help will be useful. Tight deadlines!!!
Parvathi. (
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
dear parvathi,
everything you have put in your session as the value parameter will automatically be casted to Object type as the prototype signature for putValue in HttpSession is
Httpsession.putValue(String name,Object value);
so whatever it is even it is an array,it will automatically be converted to Object type.
so if it is String[][],it will be converted to Object[][].
so you have to recast it as (String[][])HttpSession.getValue("GivenName");
i tried with respect to casting and succeded.
with regards
parvathi Viswanathan

Joined: Sep 25, 2000
Posts: 2
thanks. that worked!
best regards
I agree. Here's the link:
subject: Array in session with JSP
It's not a secret anymore!