wood burning stoves*
The moose likes Servlets and the fly likes Assigning an Array to a session attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Assigning an Array to a session attribute" Watch "Assigning an Array to a session attribute" New topic
Author

Assigning an Array to a session attribute

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
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

Joined: Feb 23, 2009
Posts: 1183
You simple set your array as a session attribute and then retrieve it back from the session later on. Don't forget to cast.


JDBCSupport - An easy to use, light-weight JDBC framework -
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

When passing between Java classes you'd just use normal Java parameter references. The session is to carry the value across requests.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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

Joined: Mar 22, 2000
Posts: 12759
    
    5
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

Joined: Nov 16, 2004
Posts: 1393
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

Joined: Aug 12, 2009
Posts: 2
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"));
 
Don't get me started about those stupid light bulbs.
 
subject: Assigning an Array to a session attribute
 
Similar Threads
Session Id coming in URL for first time
Populate Table Rows With Data from AJAX
Populating a Select List with an Array
Array as session attribute
Session scoped attributes in different apps