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().getServ letContext ().getRequ estDispatc her(Consta ntsInf.SEC URITY_LOGO UT_FILE);
sNextURL="/History.jsp";
obj_RequestDispatcher = getServletConfig().getServ letContext ().getRequ estDispatc her(sNextU RL);
//forward the page to the next screen
obj_RequestDispatcher.forw ard(reques t,response );
Trouble is I get exception java.lang.IllegalStateExce ption: Cannot forward. Response already committed.
// this makes the word document
kickOffWordDoc( Id, response );
RequestDispatcher obj_RequestDispatcher;
obj_RequestDispatcher = getServletConfig().getServ
sNextURL="/History.jsp";
obj_RequestDispatcher = getServletConfig().getServ
//forward the page to the next screen
obj_RequestDispatcher.forw
Trouble is I get exception java.lang.IllegalStateExce
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
http://www.javaworld.com/javaworld/javatips/jw-javatip136.html
ASKER
This is getting tricky, I dont want to use struts... is there any simpler way?
ASKER
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().getServ letContext ().getRequ estDispatc her(sNextU RL);
obj_RequestDispatcher.forw ard(reques t,response );
// code in the Servlet called by Word Document
RequestDispatcher obj_RequestDispatcher;
sNextURL="/History.jsp";
obj_RequestDispatcher = getServletConfig().getServ
obj_RequestDispatcher.forw
>> 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.getRequestDispatch er( "/History.jsp" );
rd .forward(request,response) ;
4 lines to 2 and much more readable :-)
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.getRequestDispatch
rd .forward(request,response)
4 lines to 2 and much more readable :-)
ASKER
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.HttpSer vletReques t request,
javax.servlet.http.HttpSer vletRespon se response)
throws javax.servlet.ServletExcep tion, 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,IOExcepti on
{
String sNextURL = "";
System.out.println("Accept WordMessag e Servlet invoked");
// Get the id of the record from the client request
String status = request.getParameter("stat us") ;
String id = request.getParameter("id") ;
FaxDAO faxdao = new FaxDAO();
faxdao.recordResponse(stat us);
try{
faxdao.UpdateTradeStatus (status, id);
System.out.println("Accept WordMessag e: Written the repsonse =" + status + " with id = " + id );
RequestDispatcher rd = request.getRequestDispatch er( "/TradeHistory.jsp" );
rd.forward(request,respons e);
}
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?
public class AcceptWordMessage extends HttpServlet{
public void doPost(
javax.servlet.http.HttpSer
javax.servlet.http.HttpSer
throws javax.servlet.ServletExcep
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,IOExcepti
{
String sNextURL = "";
System.out.println("Accept
// Get the id of the record from the client request
String status = request.getParameter("stat
String id = request.getParameter("id")
FaxDAO faxdao = new FaxDAO();
faxdao.recordResponse(stat
try{
faxdao.UpdateTradeStatus (status, id);
System.out.println("Accept
RequestDispatcher rd = request.getRequestDispatch
rd.forward(request,respons
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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.lo cation.hre f"
it will be a javascript error, it might prefer:
"opener.location.href" rahter than "window.opener.document.lo
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.getRequestDispatch
//forward the page to the next screen
obj_RequestDispatcher.forw