• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Show a progress page using Struts

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to show a interestitial or status page as there is few seconds wait before my webservice call returns results. I was trying to implement this using Threads, but i end up calling the webservice twice. First time i initiate the action method when i click on the next button. Second time i have to call the same action again so i can the status of the thread. If the status is complete then i show the next page, otherwise i show the interstitial page, which just contains a gif image and a wait message. I can do this in plain jsp by following the instruction in this link, but i cannot get around the Struts Framework to implement. Any ideas will be appreciated
http://www.precisejava.com/book/chapter1/progress_message.html

Plust there is a sleep in the thread, which causes unnecessary delay.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the functionality of the startProcess.jsp (listing 1.3) in your Struts action, something like this:

StatusBean statusBean = new StatusBean();
request.getSession().setAttribute("statusBean", statusBean);
new Thread(statusBean.start());

Then forward to progress.jsp, which can be coded exactly as in the example. Note that the statement:

setTimeout("location='Progress.jsp'", 1000);

Should reference the jsp, just as it does in the example: not the Struts action.

Another thing you need to be aware of is that in this case the ActionForm for the page needs to be put in session scope. Since you're making multiple submits to see if the process is finished, anything you put in the request is going to be gone by the time the page with the data is actually displayed

You may also want to modify the statusBean so that your ActionForm is one of it's properties. Then instantiate the ActionForm, put it in the session, and pass it to the statusBean using a setter. Then, whatever process you're running within the statusBean can put its results in to the ActionForm, which can then be used by the JSP.
[ April 14, 2006: Message edited by: Merrill Higginson ]
 
hasina kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not using ActionForm bean. I am using DynaActionForm. I tried to put the code, where i start the thread in the action class instead of having in status.jsp, but that did not make any difference. Also i cannot specify "progress.jsp" within setTimeOut javaScript function. I just get "Page Not found". I cannot get rid of setTimeOut function. If i do that, then my next page(after processing) does not show up at all. It just keeps on showing the interstitial page.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by hasina kumar:
I am not using ActionForm bean. I am using DynaActionForm.



In that case, simply define the ActionForm name to the Action that starts the thread, so that Struts will instantialte the DynaActionForm and pass it to the execute(). You can then cast it as a DynaActionForm and pass it via a setter to the status bean. In a situation like this, though, I find it easier to work with a subclass of ActionForm. That way I'm able to instantiate the form bean whenever I want. The disadvantage to a DynaActionForm is that Struts has to instantiate it.

Originally posted by hasina kumar:
Also i cannot specify "progress.jsp" within setTimeOut javaScript function. I just get "Page Not found".



This means that you are either misspelling the page name (lower case p instead of upper case?) or not specifying the correct path relative to the current context. That's easy to fix. If all else fails, just specify:

setTimeout("location='<%=request.getContextPath()%>/Progress.jsp'", 1000);

If you're putting all your JSPs inside the /WEB-INF/ directory as some developers do, then this method won't work, as no call can be made to any JSP .

In this case, you'd have to create a separate forward action just to call the JSP and have your JSP call the action instead of the JSP.

Example:

<action name="myForm" path="/progress" forward="/WEB-INF/Progress.jsp" />

Then in your Jsp:

setTimeout("location='progress.do'", 1000);
[ April 17, 2006: Message edited by: Merrill Higginson ]
 
hasina kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that is true i have all my jsp' under /WEB-INF for security reason. If i create another action in the struts-config.xml file and try to use that in setTimeOut method, i will get bean properties are not defined, since the all the bean properties are set in the action class, so if we try to point to progress.do page, it does not get bean properties from the request.

May be i am not doing this correclty. Can you show me how you would do this, when i do not have any ActionForm defined:

In that case, simply define the ActionForm name to the Action that starts the thread, so that Struts will instantialte the DynaActionForm and pass it to the execute(). You can then cast it as a DynaActionForm and pass it via a setter to the status bean. In a situation like this, though, I find it easier to work with a subclass of ActionForm. That way I'm able to instantiate the form bean whenever I want. The disadvantage to a DynaActionForm is that Struts has to instantiate it.

 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a previous Struts-based prototype that I developed I displayed a "processing" message by hiding the main section of the page and showing the section of the page with the processing message. This worked fine...at least for the prototype. It worked great with Firefox, but we noticed that with IE the animated GIF file did not animate. A quick search of my computer did not turn up the code, but it was a very simple solution.

- Brent
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it's better to show you what I mean by example. I created a small Struts application to test this concept. I've run this and verified that it works. Here are the various parts:

StatusBean.java

struts-config.xml


StartThreadAction.java



progress.jsp


You will notice that the scope for the form bean is "session". I understand that normally it is best practice to use request scope. In this instance, however, it is not possible to use request, and session is your only option.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've implemented this example and it's working fine.
But I have a question concerning the threading. I've read that if your deploying a webApp on an application server that it's not appropriate to start your own threads...

So is there another way to do this without starting a new thread ?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd only recommend the above solution in the case where you need a more "heavy duty" solution, such as a long running application that requires some sort of progress report on what is happening.

If all you want is a message saying "please wait...." and maybe an animated graphic, there are easier ways to do it. This link has one example of how to do it.

Regarding this "rule" that you shouldn't start threads in a app server, as far as I'm concernted it's one of those rules that was meant to be broken. The only thing you have to be wary of is that if this thread goes into an infinite loop, or doesn't end normally, it could bring down the server, and the server won't try to monitor it or bring it under control. As long as you write good code with plenty of exception trapping, though, I wouldn't worry too much about this.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the rule...my understanding is that you should not start threads in your EJB layer (app server) but that creating threads at the servlet layer is just fine (this is what Merrill has done).

- Brent
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic