Solved

avoiding redefinition of session scoped object

Posted on 2004-04-26
15
318 Views
Last Modified: 2010-04-01

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
0
Comment
Question by:rosshind
  • 8
  • 4
  • 3
15 Comments
 
LVL 7

Expert Comment

by:searlas
ID: 10917188
<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.
0
 

Author Comment

by:rosshind
ID: 10917195
Thanks,

is there a way to do this using JAVA?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 10917263
<%
ArrayList arrVirtOrderedProducts = (ArrayList)session.getAttribute("arrVirtOrderedProducts");
if( arrVirtOrderedProducts == null )
{
    arrVirtOrderedProducts = new ArrayList();
    session.setAttribute("arrVirtOrderedProducts", arrVirtOrderedProducts);
}
%>
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 35

Expert Comment

by:TimYates
ID: 10917270
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...
0
 
LVL 7

Expert Comment

by:searlas
ID: 10917323
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...


0
 
LVL 35

Expert Comment

by:TimYates
ID: 10917351
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);
%>
0
 

Author Comment

by:rosshind
ID: 10917680
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
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10917744
>  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... ?

;-)
0
 

Author Comment

by:rosshind
ID: 10917798
Thanks Tim, great help.

Do you use the JSP tags instead of JAVA much?  I find them somewhat inflexible.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10917843
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
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10917849
PS:  Maybe a split between me and searlas would be fair for this question?  His answer is correct too after all :-)
0
 
LVL 7

Assisted Solution

by:searlas
searlas earned 250 total points
ID: 10917993
Well, I did get the attribute wrong:
<jsp:useBean name="arrVirtOrderedProducts" class="java.util.ArrayList" scope="session"/>

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

I agree that tags can be a little obtuse when you're used to Java, but the reason I gave a (standard) JSP tag is because you asked in the JSP forum...

0
 
LVL 35

Expert Comment

by:TimYates
ID: 10918011
But

<%
  // java
%>

is standard JSP too ;-)

Hee hee
0
 

Author Comment

by:rosshind
ID: 10918291
Thanks guys.

I'm the only developer here too.  
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10918315
> 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
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now