Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forwarding response from Servlet to Jsp

 
Bunty Paul
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

response.setContentType("application/text");
response.setHeader("Content-Disposition",
"inline; filename=\"" + "test.pdf" +"\"");
response().setBufferSize(100*1024); //100KB

OutputStream fos;
fos = response().getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(data.toString().getBytes());
bos.flush();
bos.close();

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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I ask why you want to have further jsp processing on that scenario?
 
Bunty Paul
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic