Link to home
Start Free TrialLog in
Avatar of somadude
somadude

asked on

JSP (page direction) problem.

Hi,
First, my set-up is.. weblogic, ejb backend, jsp front end, with a servlet brokering requests.

My problem is that I need to execute some serverside work, as a result of a POST request, that can take up to 2 hours to complete (legacy system). Therefore the browser times out b4 it's completed......

In "pseudocode" what I (the servlet) need to do is...

* recieve and handle POST request
* display a re-freshing "please wait" page/jsp to avoid
  the browser timing out the request.
* execute the work that can take hours.
* redirect to "finished" page & display details.

The main difficultly is that you seem to be only able to call HttpServletResponse::sendRedirect() *once*, it says so in the docs.

So I call it once to display the "please wait" page, but then can't call it again to show the "finished" page.

But also, weblogic seems to ignore my request to display the "please wait" page until *after* the 2-hour long method executes! I've tried flushing the buffers but it doesn't help.

any suggestions greatly appreciated!
thanks,
sd

Avatar of ph0rman
ph0rman

One possibility would be to use javascript to create a new window just before the submit is processed.  the new window would display the "please wait" and could do refreshing to avoid timeouts.  Once the initial window is finished processing it can close that secondary window via javascript.  The down-side is that you end up with two windows rather than one.

You could also do some shenanigans using frames and create the same type of behavior if keeping it one window is important.
use thread, something like this:

public class BackgroundProcessorServlet extends HttpServlet implements Runnable {

     private static final int NOT_WORKING = 0;    
     private static final int WORKING = 1;

     Thread runner; // this is the thread that does the work
     int status;
     String strStatus;
     
     public void init(ServletConfig config) throws ServletException
     {
          super.init(config);
     
          status = NOT_WORKING;
          strStatus = "Not working";
     }


     public void doGet(HttpServletRequest req, HttpServletResponse res)
                         throws ServletException, IOException {

          res.setContentType("text/html");
          PrintWriter out = res.getWriter();

          if (status != WORKING) {
               runner = new Thread(this);
               runner.setPriority(Thread.MIN_PRIORITY); // be nice ;)
               runner.start();

               strStatus = "Working";
               status = WORKING;
          }
         
          out.println(strStatus);
          return;
     }



     public void doPost(HttpServletRequest req, HttpServletResponse res)
                         throws ServletException, IOException {
          doGet(req,res);
          return;
     }    

     public void run() {

          // make the thread do whatever you need it to do

          // when your done, set strStatus and status back to not working
     }

}    


Avatar of CEHJ
Don't know if this would work, but you could try to add this to your headers of your 'wait' screen.
<META HTTP-EQUIV=Refresh CONTENT="180; URL=http://www.your-wait-page.jsp/"> This *should* cause is to refresh every three minutes in your browser. You could maybe then get the servlet to check a variable and redirect when the data is available, otherwise simply return.
Look at this website:

http://www.jdance.com
Avatar of somadude

ASKER

CEHJ - I actually tried a something almost exactly like that a few days ago. What I did was..

* redirect to wait page
* continue processing
* wait-page checks flag in session to see if processing is
  finished, if finished, redirect to finish-page.
* processing is finished, flag is set in session

I thought this was going to work, but there are 2 problems

1) you can only call response.sendRedirect once
2) alot of the time, weblogic won't even display the
   wait-page until the server-side processing is
   finished. ??
As far as the problems are concerned:

1) Surely you only need to redirect once per page - once to the 'wait' page, then once to the 'finished' page (redirected *by* the wait page)?

2) You are doing the processing in another thread aren't you?

Of course, you really need messaging paradigms for this sort of thing...
ASKER CERTIFIED SOLUTION
Avatar of saxaboo
saxaboo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd go along with Saxaboo on this one, as the last line of my previous message suggests.

You could even implement a quick and dirty email solution!
saxaboo - yeah I agree with you, I actually started checking out JMS this morning, looks like it could be the ticket.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: saxaboo
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
Per recommendation, force-accepted.

Netminder
EE Admin