Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between setstatus(500) and senderror(500)

When writing servlet, I could use response.setStatus(500) and response.sendError(500), what's the difference between those two?

like image 963
Adam Lee Avatar asked May 26 '17 15:05

Adam Lee


2 Answers

There's another subtle difference between the two methods at least in Servlet 2.4+:

response.sendError(500) will redirect to the configured error page (in web.xml) for that status code whereas response.setStatus(500) assumes you are providing the response body and the container does not check for a matching error page declaration.

like image 79
Yeroc Avatar answered Nov 20 '22 07:11

Yeroc


setStatus can be overruled by a consecutive setStatus with another code. In contrast, sendError immediately sends the error code to the client and cannot be undone. For errors, you should rather use sendError.

https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html#sendError(int)

like image 4
Erich Kitzmueller Avatar answered Nov 20 '22 05:11

Erich Kitzmueller