Put arrays in session variables

Do you know how we could store an array in something like sessions.

Actually I’ve got a m by n array and I need to stroe all of its n*m elements in the other pages.

Any idea?

Of course besides creating n*m session variables,:D, which does not make much sense for large values of m and n.

$_SESSION[‘arr’] = $your_array; ??

$_SESSION[‘arr’] = $your_array;

that’s php, but you can do similar in Java


  Object [] [] obj = new Object[10][10];
  
  // populate
  
  HttpSession session = request.getSession();
  
  session.setAttribute("myArray", obj);
  

Thanks, by

[obj[0][0]="xx";obj[0][1]="YY";...,obj[9][9]="uu";

I could feed the array. And with

session.setAttribute("myArray", obj);

, as you said, put it in the session. But how may I supposed to retain the elements of the array at my page. It is an object not an array now.

In Java, an Array is an Object.

Oops, I just came across this post by making a search for something (related to php), didn’t read carefully enough that it was about java :slight_smile:

rushiko, you’re right. In java an array is an object. Output is


         Object xxx=(String)session.getValue("myArray");///to get the array

now how you’ll find the elements of the first settled array.

Because an Array is an Object it may be passed around using the Parent class Object, however, when you need to use Array specific operations and/or methods, you’ll need to cast it as an Array.
So:
//in servlet
String array = new String{…};
session.setAttribute( “myArray”, array );

//in jsp - your code
Object xxx=(String)session.getValue(“myArray”);///to get the array
Compile warning: method getValue has been deprecated - use getAttribute
Runtime error: ClassCastException: a String is not a String

// in jsp - my code
String array = (String)session.getAttribute( “myArray” );

// in jsp - long form
Object obj = session.getAttribute( “myArray” );
String array = (String)obj;

You can store any serializable Java objects, even your Dtata Transfer Objects (DTO - should implement Serializable) in the session using the setAttribute & getAttribute methods. You could also save in a request scope with the SetAttribute() method.

briefly,
first


///in servlet
session.setAttribute( "myArray", array );

second


///in jsp
String[][] array = (String[][])session.getAttribute( "myArray" );

Basically would work and then full access to the elements of the set_attributed array.
Am I right? Should we have n,m for the array[n][m] in the jsp now?

I’m not quite sure what you’re asking…

Are you asking if the array you get in the JSP is the same object that you set in the servlet?

If so, yes.

Fine!, How do we obtain the size of this array then?

The same way you do any other time you have an array: array.length

what about a two dimensional one! You know the output


String[][] array = new String[13][12];
int a_l=array.length;
System.out.print("a_length"+a_l);

is 13.

A 2-Dimensional array is an ‘array of arrays’, so, in your case:
array.length = 13
array[n].length = 12

and what is n?

n is a mathmatical notation used to represent an unknown number.

In this case, n represents a number from 0 to 12, ie: the elements in the first dimension of your array.

Thank you.
And this goes on and on with no upper bound. Is that true?