Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-05-04
10
Medium Priority
?
334 Views
Last Modified: 2008-01-09
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.

0
Comment
Question by:jtregjtreg
  • 5
  • 5
10 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13928758
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);  
0
 

Author Comment

by:jtregjtreg
ID: 13933616
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.
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13933811
Use a "Synchronizer Token pattern" to prevent repeat submission.

http://www.javaworld.com/javaworld/javatips/jw-javatip136.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jtregjtreg
ID: 13933834
This is getting tricky, I dont want to use struts... is there any simpler way?

0
 

Author Comment

by:jtregjtreg
ID: 13933883
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);
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13933966
>> 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 :-)
0
 

Author Comment

by:jtregjtreg
ID: 13934023
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?
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 500 total points
ID: 13934056
Yes. If you use a pop-up to hold the word document you can call some javascript to reload/forwad the page:

here's a proof of concept:

popup_parent.jsp
---------------
<%@ page language="java" contentType="text/html" %>
<html>
</head>
<title>
Parent
</title>
<script type="text/javascript">
function popme(){
      window.open("popup_child.jsp?page=1234","Pop UP","scrollbars=yes,resizable=yes,location=no,width=150,height=150");
}
</script>
</head>
<body>
page param?:<%=request.getParameter("page")%><br>
Click <a href="javascript:popme()">here</a> for popup
</body>
</html>

popup_child.jsp
----------------
<%@ page language="java" contentType="text/html" %>
<html>
</head>
<title>Child</title>
<script type="text/javascript">
function popback(){
      window.opener.document.location.href = "popup_parent.jsp?page=bertandernie" ;//change parent
      self.location.href = "popup_child.jsp?page=bananas" ; //change self e.g. submit request
}
</script>
</head>
<body>
Thanks <%=request.getParameter("page")%>,
Click <a href="javascript:popback()">here</a> to kick everything off
</body>
</html>
-------------

The important javascrip being popback()
0
 

Author Comment

by:jtregjtreg
ID: 13936570
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
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13936853
>>but not with Explorer!

it will be a javascript error, it might prefer:

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
This applies to Dell but may also apply to other manufacturers as well. We ran across a few machines that just dropped recently it trust relationship with the server. After doing the basic removing and joining the domain again, it changed to No logo…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

577 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