aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setStatus and sendError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "setStatus and sendError" Watch "setStatus and sendError" New topic
Author

setStatus and sendError

Andy Smith
Ranch Hand

Joined: Sep 28, 2003
Posts: 239
Wats the diffenece between the two methods ??
and when either of them is to be used?


Share Knowledge to gain it.
SCJP 2, SCWCD 2, SCDJWS, IBM 141 (In Progress), IBM 486 (Next)
michael brosnahan
Greenhorn

Joined: Mar 18, 2002
Posts: 9
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.
Pranil Kanderi
Greenhorn

Joined: Nov 21, 2003
Posts: 4
More importantly, unlike setStatus, after the sendError is invoked the response is committed and any attempt to use it will throw an IllegalStateException.


SCJP,SCWCD,SCBCD
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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.


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Pranil Kanderi
Greenhorn

Joined: Nov 21, 2003
Posts: 4
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

Joined: Sep 28, 2003
Posts: 239
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

Joined: Jun 08, 2002
Posts: 3178
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setStatus and sendError