File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes forwarding response from Servlet to Jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "forwarding response from Servlet to Jsp" Watch "forwarding response from Servlet to Jsp" New topic

forwarding response from Servlet to Jsp

Bunty Paul

Joined: Jun 26, 2006
Posts: 28
I have a problem . I have a screen where when i press a button it goes to the servlet , and there i am constructing a rtf or pdf file , then writing to the output stream and closing it. But after that i want to go back to the same jsp and display some message .

"inline; filename=\"" + "test.pdf" +"\"");
response().setBufferSize(100*1024); //100KB

OutputStream fos;
fos = response().getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(fos);

after this i have to forward the response to jsp.

I get the exception : java.lang.IllegalStateException: Cannot forward after response has been committed

I am using struts framework.

I know a response cannot be forwarded to jsp after closing the stream

but still i want to acheive this requirement somehow.

is there any other way of doing it. i believe nothing is impossible
Manoj Paul
Ranch Hand

Joined: Jan 18, 2007
Posts: 112
How could you say "nothing is impossible"

I have done "nothing" in the past 24 hours....
And believe me "Its possible"
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2068
May I ask why you want to have further jsp processing on that scenario?
Bunty Paul

Joined: Jun 26, 2006
Posts: 28
Originally posted by Jesus Angeles:
May I ask why you want to have further jsp processing on that scenario?

Thats the requirement for the project i am working on.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Can you please elaborate your requirement? Like what kind of message you want to display using a JSP page? And if its like that then why are you throwing the output to the user using response object in your first place?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Guess what? It is impossible.

You cannot return two responses for one request.

What is possible is to forward to the JSP, and then using Javascript on the page, cause a request to a servlet to fetch the PDF.

Setting the content-disposition correctly (search through this forum for examples) will cause the JSP to remain displayed while the user is presented with the download dialog.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: forwarding response from Servlet to Jsp
It's not a secret anymore!