This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem forwarding a to jsp in struts

 
Aadil Pala
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to writed a couple of zip files using the output stream but after that I need to move to another JSP by using the ActionForward but it does not work
My code
ServletOutputStream stream = response.getOutputStream();
stream.print( zipFile );
stream.flush();
stream.close();
return actionMapping.findForward(FORWARD_SUCCESS);

Is there an alternative approach using which I can forward it.
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the problem is that, once you've written to the output stream, you can't redirect to another location. In essence, you've already generated a response - redirecting to a JSP to create "more response" leads to an exception (IllegalState, if I'm not mistaken).

So, what's the solution? Good question. You could generate the reply HTML in your Action class, but I don't really like that solution. Not only does it screw up your architecture, it's just plain ugly.

Maybe someone else has a good solution. Sorry.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't really say what problem you're having...

As a guess, make sure that the value of FORWARD_SUCCESS is equal to the name of one of the forwards you have defined in your struts-config.xml for this action.
 
Varun Narang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
I think Aadil Pala is quite clear with the problem.
When you have already written to the response's output stream, you can't redirect to anyother url, and if you do so, you will get a big fat-IllegalStateException.
I am also looking for the solution of the similar kind of problem. If anyone can please help.
Regards,
Varun Narang.
 
Manesh Kumar
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aadil Pala:
I am trying to writed a couple of zip files using the output stream but after that I need to move to another JSP by using the ActionForward but it does not work
My code
ServletOutputStream stream = response.getOutputStream();
stream.print( zipFile );
stream.flush();
stream.close();
return actionMapping.findForward(FORWARD_SUCCESS);

Is there an alternative approach using which I can forward it.
Thanks


Are you trying to download the zip file?
Moreover, since you have getOuputStream() on response object, JSP will thrown an exception (IllefalStateException) because it will try to call getWriter() on the response after the getOuputStream().

What are you going to do in JSP?.

If this is just for downloading you can return null, which will make the Struts to stop forwarding.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One common solution to this situation is to forward/redirect to the target page first. Then, using javascript, fire off another request for the file to be download from the onload event of that target page.

You may also want to put a message on the target page saying something like
"Your download should begin shortly. If it doesn't, click HERE." with HERE being a link to the file. This will insure that people with Javascript disabled can still retrieve your file.
 
Naik Priyanks
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution could be writing the file downloading code in a servlet instead of a JSP.
[ March 27, 2006: Message edited by: Naik Priyanks ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic