Link to home
Start Free TrialLog in
Avatar of rosshind
rosshind

asked on

avoiding redefinition of session scoped object


I'm not sure how to define an object with session scope the first time a page is viewed and ensure that the same in scope object can be displayed on the same page again after data has been loaded into it..

Below is how I'm defining the object (you can see by the comments I';m not sure what I'm doing.

//ArrayList arrVirtOrderedProducts = new ArrayList();
//request.getSession().setAttribute("arrVirtOrderedProducts", arrVirtOrderedProducts);

ArrayList arrVirtOrderedProducts = (ArrayList) request.getSession().getAttribute("arrVirtOrderedProducts");


thanks for all of your help
Avatar of searlas
searlas

<jsp:useBean name="arrVirtOrderedProducts" class="java.util.ArrayList" scope="session"/>

This will create the ArrayList if and only if it does not already exist in the session.
Avatar of rosshind

ASKER

Thanks,

is there a way to do this using JAVA?
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that will create the array list if it doesn't exist in the session...

so after that code, arrVirtOrderedProducts will always be the ArrayList stored in the session...
rosshind, I'm wondering why you emphasized JAVA?  Is that because you think this won't work?
<jsp:useBean name="arrVirtOrderedProducts" class="java.util.ArrayList" scope="session"/>

TimYates answer does show how to write this in long-hand, but it's best-practice to minimize
the amount of java embedded inside a JSP (therefore minimizing maintenance) and that's
the whole reason for JSP tags like jsp:useBean, jsp:setProperty etc...


And...my code was a bit wrong...should have been this:

<%
    ArrayList arrVirtOrderedProducts = (ArrayList)session.getAttribute("arrVirtOrderedProducts");
    if( arrVirtOrderedProducts == null )
        arrVirtOrderedProducts = new ArrayList();
    session.setAttribute("arrVirtOrderedProducts", arrVirtOrderedProducts);
%>
Hi serlas,

Your code doesnt seem to work but thats not the reason.  There is some complexity to my presentation layer and I'm more comfortable using JAVA.  JSP may be clearer to you, but not to me, I actually find it quite limited.

>minimize the amount of java embedded inside a JSP (therefore minimizing maintenance)

I dont accept that this is the case in EVERY case (but in general, sure).  Complex aspects of the presentation layer may require some complexity to be revealed in order to facilitate maintenence.

Thanks for your comment though
>  And...my code was a bit wrong...

Heh, no it wasn't...the first version was right ;-)

God, you can tell it's a Monday....  Now where did I put my brain... ?

;-)
Thanks Tim, great help.

Do you use the JSP tags instead of JAVA much?  I find them somewhat inflexible.
I don't really do jsp tags...

I do use Struts though, and write my own Tags sometimes :-)

http://jakarta.apache.org/struts
http://www.smartframeworks.com/qt-jsp-customtags.html

I know what you mean though...but I am currently the only developer in the team..

One good thing with using tags (and things like struts), is that it takes a lot of the code out of the JSP page, allowing less-technical more design based people to understand how the pages work (rather than a lot of <% blah %> stuff scaring them) ;-)

It's just a matter of preference though...  Sometimes, jsptags just don't cut it :-/

Good Luck!!

Tim
PS:  Maybe a split between me and searlas would be fair for this question?  His answer is correct too after all :-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But

<%
  // java
%>

is standard JSP too ;-)

Hee hee
Thanks guys.

I'm the only developer here too.  
> I'm the only developer here too.  

Hehehe, the worst bit is when you are done, and it all works, and the only comment you get is something like "I thought we agreed the background should be green" ;-)

Good luck with it!!

Tim