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
Solved

Storing an attribute in application scope with Struts?

Posted on 2004-04-14
32
1,661 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 125 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 75 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

809 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