• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setStatus and sendError

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wats the diffenece between the two methods ??
and when either of them is to be used?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The setStatus method is used to set the return status code when there is no error (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY).
sendError sends an error response to the client.
If you have declared an error-page for the particular error code in the web.xml file that error page will be sent to the client.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More importantly, unlike setStatus, after the sendError is invoked the response is committed and any attempt to use it will throw an IllegalStateException.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you overlooked the Servlet Specification... I don't think it will throw IllegalStateException.... Instead it will ignore the data, after the methods are called.....
You may want to view the following passage from servlet 2.4 specification, page #51

These methods will have the side effect of committing the response, if it has
not already been committed, and terminating it. No further output to the client
should be made by the servlet after these methods are called. If data is written to
the response after these methods are called, the data is ignored.

 
Pranil Kanderi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right it doesnot throw an IllegalStateException.
I read that from the SCWCD Study Kit. Page 96, last paragraph says:

Both methods throw a java.lang.IllegalStateException if the response is already committed. Similarly, after calling these methods, the response should be assumed to be committed, and no other data should be written to the output stream.


BUT, I tried this (modified example from SCWCD study kit chapter6) on Tomcat and it did NOT ignore the output. ???
public void service(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("Following are the application (or ServletContext) initialization parameters:<br>");
ServletContext context = getServletConfig().getServletContext();
Enumeration enum = context.getInitParameterNames();
while(enum.hasMoreElements())
{
String name = (String) enum.nextElement();
String val = context.getInitParameter(name);
pw.println(name+" = "+val+"<br>");
}
pw.println("</body>");
pw.println("</html>");


}
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i tried
"pResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);"
before sending the response on TOMCAT
and i got this after the desires response ...
Apache Tomcat/4.0.1 - HTTP Status 500 - Internal Server Error
--------------------------------------------------------------------------------
type Status report
message Internal Server Error
description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.
--------------------------------------------------------------------------------
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pranil Kanderi:

BUT, I tried this (modified example from SCWCD study kit chapter6) on Tomcat and it did NOT ignore the output. ???


I think sometimes servlet containers cannot do exact things according to the servlet specification... Then we got such doubts... That's too bad...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic