4. Designing and Developing Servlets to Handle Server-Side Exceptions
4.1. For each of the following cases, identify correctly constructed code for handling business logic exceptions, and match that code with correct statements about the code's behavior:
4.1.1. return an http error using setStatus
4.1.1.1.
public void HttpServletResponse.setStatus(int statusCode)
4.1.1.2. if this is not called, the server by default sets
the status code to SC_OK(200).
4.1.1.3. example status
codes: HttpServletResponse.SC_OK(200), SC_NOT_FOUND(404),
SC_NO_CONTENT, SC_MOVED_TEMPORARILY/PERMANENTLY, SC_UNAUTHORIZED,
SC_INTERNAL_SERVER_ERROR, SC_NOT_IMPLEMENTED, SC_SERVICE_UNAVAILABLE
4.1.1.4. calling setStatus() on an error leaves a servlet
with the responsibility of generating the error page
4.1.1.5. must be called before the response is committed,
otherwise call is ignored
4.1.2. return an http error
using sendError
4.1.2.1. public void HttpServletResponse.sendError(int
statusCode[, String statusMessage]) throws IllegalStateException,
IOException
4.1.2.2. the sendError() method causes the
server to generate and send an appropriate server-specific
page describing the error (unless <error-page> defined
in web.xml)
4.1.2.3. with the two argument version of
this method, the server may include the status message in
the error page, depending on the server implementation
4.1.2.4. must be called before response body is committed,
else throws IllegalStateException