When writing servlet, I could use
response.setStatus(500) and response.sendError(500), what's the difference between those two?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With