?
Solved

Storing an attribute in application scope with Struts?

Posted on 2004-04-14
32
Medium Priority
?
1,668 Views
Last Modified: 2013-11-23
Hi,

I'm trying to store an Enumeration in a Struts action such that its repeatedly available to a JSP (in order to reduce db access).

However, the Enumeration only appears to be available the first time the jsp is loaded. Subsequent use of the jsp results in a drop-down list with no entries.

Here is the code I'm using:

(In the action)
Enumeration estaff = staff.elements();
request.getSession().getServletContext().setAttribute("estaff", estaff);

(In the jsp)
<%
      Enumeration estaff = (Enumeration) getServletContext().getAttribute("estaff");
%>

<html:form action="signin.do" focus="username">
<html:select property="visitingName">
  <html:options collection="estaff" labelProperty="staffName"
property="staffId"/>
</html:select>
</html:form>

Can you tell me why this is happening and how I can resolve it.

Cheers,

Steve
0
Comment
Question by:stevebeech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 7
  • 6
  • +2
32 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822136
>> request.getSession().getServletContext().setAttribute("estaff", estaff);

Are you sure that "estaff" have value when you put it in the context.


0
 

Author Comment

by:stevebeech
ID: 10822155
Yes, its an Enumeration created from a Vector (of JavaBeans populated from a db request). I'm sure its there because the first time the jsp is viewed (directly after the Action) there is data in the drop-down box.

The Action which puts the Enumeration in the context is only called once.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822178
That's great.  Can you just print the enumeration each time you read it from the context.  So that we can either tool into the tag or the object that stored in the context.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stevebeech
ID: 10822252
Right,

I've replaced the html:select tag with the following:

<%
      while (estaff.hasMoreElements()) {
           out.println(estaff.nextElement());
      }
%>

Again, the enumeration is only printed out on the first view of the jsp. !?!
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822264
Hi, What I am suspecting is that, all your data that goes into the context might not be serialized.  That might be the cause.

Can you serialize the elements that you store in a vector and try again.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822275
No few servers are not keeping all the session/context data in memory.  They are having its own persistance mechnism.  So all the server vendors are recommending to store the serialized object in the session.

So better serialize your objects and store in the session/context.


Regards,
Muruga
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822277
>>> No few servers

typo, Now few servers ....
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 10822341
An Enumeration can generally only be used once (i.e. enumerated once). You'd be better using some List implementation
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822358
You are absolutely correct CEHJ.

Hi better store your Vector itself in the context.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822362
each and every time,

(In the jsp)
<%
     Enumeration estaff = ((Vector) getServletContext().getAttribute("estaff")).elements();
%>


Regards,
Muruga
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822369
(In the action)
request.getSession().getServletContext().setAttribute("estaff", staff);
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 300 total points
ID: 10822379
You can iterate only once in Enumeration/Iteration.

When you try to iterate for the second time, the control would be at the end of elements.

So hasMoreElements() will return false.

You won't get any elements there after.  

Points to CEHJ


Regards,
Muruga

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822380
Just to make it absolutely clear, 'estaff' is now a Vector [better use 'vstaff' perhaps ;-)]
0
 

Author Comment

by:stevebeech
ID: 10822407
I was wondering if that was the problem. I've previously used a vectors in the request scope and then created an Enumeration in the jsp page with the following code (well, similar):

<%
while (vc.hasMoreElements()) {
      ClientForm key = (ClientForm) vc.nextElement();
      out.println("<OPTION value='"+key.getClientId()+"'>");
      out.println(key.getCompanyName());
      out.println("</OPTION>");
}
%>

but thought that this was too messy.

Your solution is a lot nicer.

I'll add some more points and award the original 125 to CEHJ, plus another 75 to you, Muruga, for your excellent effort, if thats okay with you both.

Much appreciation,

Steve
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822422
Itz our pleasure...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10822428
BTW, why do you need an enumeration to loop through a vector? You cah directly use:

for ( int i = 0, iSize = vector.size () ; i < iSize ; i ++ )
{
     ClientForm key = (ClientForm) vector.get ( i ) ;
     out.println("<OPTION value='"+key.getClientId()+"'>");
     out.println(key.getCompanyName());
     out.println("</OPTION>");

}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10822437
Oops, already closed ;-) sorry for disturbing a PAQed thread.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822438
8-)
0
 

Author Comment

by:stevebeech
ID: 10822452
Seems like I accepted Muruga's answer and put CEHJ's as an assisted answer when it should have been the other way round. Does this matter?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822462
But I got a mail as Good Assist!  I donno....  CEHJ what do you say
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822464
Not really ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10822471
It matters for people who would come looking to this page, searching for solutions. If they don't want to read all comments on the page, they would click on the "See Solution" link at the top (near the question) and would be directed to Muruga's comment.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822472
Everyone gets mail starting 'Good assist..' . Charming of you both to be concerned, but don't worry 8-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10822473
>> But I got a mail as Good Assist!

Whenever there is a split, everyone gets a mail-alert as "Good Assist" only.
0
 

Author Comment

by:stevebeech
ID: 10822476
NIce one. Cheers!

Steve
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822501
I am really expecting CEHJ to get the points for finding out a mistake.  
Thanks CEHJ for giving away your points to me.

;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822571
>>Thanks CEHJ for giving away your points to me.

I'm not giving them away! I agree you added value to this and you deserve them

>>
they would click on the "See Solution" link at the top (near the question) and would be directed to Muruga's comment.
>>

which is definitely a piece of added value that expands on my terse explanation ;-)
0
 
LVL 7

Expert Comment

by:searlas
ID: 10822796
Put the Vector (staff) in the session instead of the Enumeration (estaff)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822846
?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10823066
Perhaps he didn't refresh for an hour ;-)
0
 
LVL 7

Expert Comment

by:searlas
ID: 10823160
That'll be it.... just had lunch...
0
 

Author Comment

by:stevebeech
ID: 10823186
Turns out I don't need an Enumeration at all:

(in the Action)
request.getSession().getServletContext().setAttribute("vstaff", staff);

(in the JSP)
<%
      Vector vstaff = (Vector) getServletContext().getAttribute("vstaff");
%>

(and to create the select box in the JSP):

<html:select property="visitingName">
  <html:options collection="vstaff" labelProperty="staffName"
property="staffId"/>
</html:select>

I hadn't realised the html:options collection attribute could refer directly to a Vector. I think this was what you were making clear CEHJ, but I used Muruga's code with the Enumeration initially.
However, this caused some problems when I tried to detect the presence of the collection with the logic:notPresent tag, due to requiring an initialisation stage first.
So, now I've got the following code before I try to retrieve the Vector from the application scope:

<logic:notPresent name="vstaff" scope="application">
      <logic:redirect href="initialise.do"/>
</logic:notPresent>

That's it. No more comments. Thanks all.
Cheers, Steve   :o)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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