File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes problem forwarding a to jsp in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem forwarding a to jsp in struts" Watch "problem forwarding a to jsp in struts" New topic
Author

problem forwarding a to jsp in struts

Aadil Pala
Greenhorn

Joined: Nov 01, 2004
Posts: 3
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

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
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

Joined: Nov 19, 2004
Posts: 30
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.


Your computer system is like AC, it's of no use when you open Windows ;)
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
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.


Manesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Naik Priyanks
Greenhorn

Joined: Nov 15, 2004
Posts: 10
The solution could be writing the file downloading code in a servlet instead of a JSP.
[ March 27, 2006: Message edited by: Naik Priyanks ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem forwarding a to jsp in struts