Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning an Array to a session attribute

 
Steve Dyke
Ranch Hand
Posts: 1622
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an array of manufacturing days in my web app at the intialization of the app. I am trying to assign this array to a session attribute so I can use it in other areas of my app. How can I get this to work?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You simple set your array as a session attribute and then retrieve it back from the session later on. Don't forget to cast.
 
Steve Dyke
Ranch Hand
Posts: 1622
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:You simple set your array as a session attribute and then retrieve it back from the session later on. Don't forget to cast.


Here is my code to make the assignment:



Where I pass this to another java class how do I get the element values?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When passing between Java classes you'd just use normal Java parameter references. The session is to carry the value across requests.
 
Steve Dyke
Ranch Hand
Posts: 1622
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:When passing between Java classes you'd just use normal Java parameter references. The session is to carry the value across requests.


So if from another servlet I get the session attribute

how do I assign this so I can pass it as array to my class? session.getAttribute("dateNumberArray")

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with assigning -- just normal Java.

The fact that you've put something in the session has no bearing on its ability to be passed around just like any other Java object instance.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Important note: all objects having references in a session should be Serializable. The servlet container has the option to serialize sessions out to disk to save memory at any time.

Bill
 
Steve Dyke
Ranch Hand
Posts: 1622
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It has nothing to do with assigning -- just normal Java.

The fact that you've put something in the session has no bearing on its ability to be passed around just like any other Java object instance.


I am still not getting this.

If my class argument is String[] arg1 what is the syntax I use from the servlet when I call my class?

Myclass myclass = new Myclass(session.getAttribute("dates")); does not work
 
Mike Giddens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my class argument is String[] arg1 what is the syntax I use from the servlet when I call my class?

Myclass myclass = new Myclass(session.getAttribute("dates")); does not work


Since .getAttribute() returns Object, you'll have to cast to a string array :
Myclass myclass = new Myclass((String[])session.getAttribute("dates"));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic