Link to home
Start Free TrialLog in
Avatar of jtregjtreg
jtregjtreg

asked on

Forwarding after I make a word document from a servlet - I cannot forward to the next jsp

I have a Word document that I create in a servlet with ServletOutputStream, that is not the problem... the thing is I want to direct the user onto another web page when the word document has been opened ...

// this makes the word document
kickOffWordDoc( Id, response );
            
RequestDispatcher obj_RequestDispatcher;
obj_RequestDispatcher = getServletConfig().getServletContext().getRequestDispatcher(ConstantsInf.SECURITY_LOGOUT_FILE);
sNextURL="/History.jsp";
obj_RequestDispatcher = getServletConfig().getServletContext().getRequestDispatcher(sNextURL);
//forward the page to the next screen
obj_RequestDispatcher.forward(request,response);      

Trouble is I get exception java.lang.IllegalStateException: Cannot forward. Response already committed.

Avatar of bloodredsun
bloodredsun
Flag of Australia image

Once you have written to the response, you cannot then forward the response. This includes settting any headers and any out.print() statements.

That is why so many web sites create a pop up window to download the document while the parent window can still retain control. There is no way to do what you are trying to do as the code stands above but you can just create a pop up that does the kickOffWordDoc( Id, response ); section if you pass any information in the session temporarily.

By the way, your code could be a lot shorter:

sNextURL="/History.jsp";
RequestDispatcher  obj_RequestDispatcher = request.getRequestDispatcher(sNextURL);
//forward the page to the next screen
obj_RequestDispatcher.forward(request,response);  
Avatar of jtregjtreg
jtregjtreg

ASKER

Ok, if that is the case then can you suggest a way of preventing the user from resubmitting the same page as I dont want them sending off another Word document.
Use a "Synchronizer Token pattern" to prevent repeat submission.

http://www.javaworld.com/javaworld/javatips/jw-javatip136.html
This is getting tricky, I dont want to use struts... is there any simpler way?

The word document will send a message back to a servlet when it is closed - I have tried using the RequestDispatcher, but that did not work... I am puzzled why I cannot do this instead

// code in the Servlet called by Word Document
RequestDispatcher obj_RequestDispatcher;
sNextURL="/History.jsp";
obj_RequestDispatcher = getServletConfig().getServletContext().getRequestDispatcher(sNextURL);
obj_RequestDispatcher.forward(request,response);
>> This is getting tricky, I dont want to use struts... is there any simpler way?

I'm not sugestting you use struts, just the theory of the synchronizer token.

>>The word document will send a message back to a servlet when it is closed - I have tried using the RequestDispatcher, but that did not work... I am puzzled why I cannot do this instead

Without seeing the rest of your code I can't so why not. What error message are you getting?
I still think your code is too complicated. What's wrong with:

RequestDispatcher rd = request.getRequestDispatcher( "/History.jsp" );
rd .forward(request,response);

4 lines to 2 and much more readable :-)
It seems to try and forward the Word document on ... the original word doc disappears...

public class AcceptWordMessage  extends HttpServlet{
      
      public void doPost(
            javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, java.io.IOException {
            performTask(request, response);
      }
      
      public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                  doPost(request,response);
      }

      private void performTask(
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException,IOException
      {
            String sNextURL = "";
            System.out.println("AcceptWordMessage Servlet invoked");
                  
            // Get the id of the record from the client request
            String status =  request.getParameter("status") ;
            String id =  request.getParameter("id");
            FaxDAO faxdao = new FaxDAO();
            faxdao.recordResponse(status);
            
            try{
                  faxdao.UpdateTradeStatus (status, id);
                  System.out.println("AcceptWordMessage: Written the repsonse =" + status + " with id = " + id );
                  RequestDispatcher rd = request.getRequestDispatcher( "/TradeHistory.jsp" );
                  rd.forward(request,response);  
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
                        
      }


I was hoping that the original Page that submitted the Word document could be redirected, but this is not possible...

I am still stuck. Is there no way in javascript to move the web page on, at the same time the word document is created?
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

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
nearly there.. I got the popup working, with FireFox it forwards me to the jsp page I want....

but not with Explorer!

Also

How can I stop the user clicking on the page before I have redirected to the new page? Like a modal popup?

I cant keep asking questions! You have earnt them already....
please help I will award you the points anyway

Best regards jtreg
>>but not with Explorer!

it will be a javascript error, it might prefer:

"opener.location.href" rahter than  "window.opener.document.location.href"