Solved

Storing an attribute in application scope with Struts?

Posted on 2004-04-14
32
1,654 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
 

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:mayankeagle
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 32
recursion example 16 70
backtracking recursion  code 19 40
Java Server Faces parameter pass? 6 24
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

762 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

26 Experts available now in Live!

Get 1:1 Help Now