Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

respnsce.setContentType()

 
Prabodhini Gatthu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On error I am trying to setContentTpe as html/text
I am getting following error
java.lang.IllegalStateException: ERROR: Cannot set header. Response already committed.
any help???
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe content type must be specified before any response is sent. Besides, it is - text/html.
- Manish
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Prabodhini. It seems like your response is already committed to the client. Have u traced ur servlet up from the code that you set the content type?
 
Prabodhini Gatthu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am setting content type to as follows
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
pdfGenerator.saveDetailsData(pdfData,out);
if there is error in pdfGenerator I am setting content type as text/html and forwarding request to error page...
Any help for this situation
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you must find some way to test the pdfgenerator before you set the header. catch any exceptions that occur before this and switch the content type but once you call pdfGenerator.saveDetailsData(pdfData,out); i think you are too committed and should just exit gracefully instead of trying to give an html error to the user
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prabodhini Gatthu:
Hi All,
I am setting content type to as follows
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
pdfGenerator.saveDetailsData(pdfData,out);
if there is error in pdfGenerator I am setting content type as text/html and forwarding request to error page...
Any help for this situation

I think we need more code snippets to solve this problem... One thing! Why don't u try to solve the error problem in saveDetailsData method of pdfGenerator itself? It is better, I think...
 
Prabodhini Gatthu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error shown now is javax.servlet.ServletException: non-HTTP request or response when I try to forward request to error page
code is similar to this
try
{
out = response.getOutputStream();
}
catch(Exception exception)
{
}
try
{
pdfGenerator.saveDetailsData(pdfData,out);
}
catch (Exception e)
{
send to error page
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the out variable is partially done in the pdfGenerator.saveDetailsData method, so it cannot be forwarded again... So the best way is, as I mentioned earlier, to handle the exceptions in the pdfGenerator.saveDetailsData method...
I'm sorry that I cannot provide you the complete correction... But I will do my best to help you out here..
 
Prabodhini Gatthu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got solution!!!
I used
response.reset();
response.setContentType("text/html");
before forwarding to error page
Thanks all for their kind help!!!
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prabodhini Gatthu:
I got solution!!!
I used
response.reset();
response.setContentType("text/html");
before forwarding to error page
Thanks all for their kind help!!!

That's great, Prabodhini... I got real valuable knowledge from this thread as well... It's really worthful to know that we can reset the content type of a response...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!