SECTION 4: DESIGNING AND DEVELOPING SERVLETS TO HANDLE SERVER-SIDE EXCEPTIONS
| Section 4 | |
|
|
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: Return an HTTP error using the sendError response method; Return an HTTP error using the setStatus method.
|
|
4.2 Given a set of business logic exceptions, identify the following: The configuration that the deployment descriptor uses to handle each exception; How to use a RequestDispatcher to forward the request to an error page; Specify the handling declaratively in the deployment descriptor.
|
|
4.3 Identify the method used for the following: Write a message to the WebApp log; Write a message and an exception to the WebApp log.
|
Section 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: Return an HTTP error using the sendError
response method; Return an HTTP error using the setStatus method.
return an http error using sendError
- public void HttpServletResponse.sendError
(int statusCode[, String statusMessage]) throws IllegalStateException, IOException
- 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)
- with the two-argument version of this method, the server may include the status
message in the error page, depending on the server implementation
- must be called before response body is committed, else throws
IllegalStateException
return an http error using setStatus
- public void HttpServletResponse.setStatus(int statusCode)
- if this is not called, the server by default sets the status code to SC_OK(200).
- example status codes:
HttpServletResponse.SC_OK(200),
SC_NOT_FOUND(404),
SC_NO_CONTENT(204),
SC_MOVED_TEMPORARILY/PERMANENTLY(302/301),
SC_UNAUTHORIZED(401),
SC_INTERNAL_SERVER_ERROR(500),
SC_NOT_IMPLEMENTED(501),
SC_SERVICE_UNAVAILABLE(503)
- calling setStatus() on an error leaves a servlet with the responsibility of
generating the error page
- must be called before the response is committed, otherwise call is ignored
4.2 Given a set of business logic exceptions, identify
the following: The configuration that the deployment descriptor uses to handle each
exception; How to use a RequestDispatcher to forward the request to an error page;
Specify the handling declaratively in the deployment descriptor.
configuring deployment descriptor for error handling
configuring deployment descriptor for exception handling
using RequestDispatcher to forward to an error page: see section 1.6 above for details
forward:
- public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException
- Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response. The forwarding servlet generates no output, but may set headers.
- The ServletRequest object has its path attributes adjusted to match the path of the target resource. Any new request parameters are added to the original.
- forward() should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
- The request and response parameters must be the same objects as were passed to the calling servlet's service method.
- Information can be passed to target using attached query string or using request attributes set with setAttribute() method.
- forwarding to an html page containing relative url's included (e.g. "IMG" tags) is a bad idea, because forward() does not notify client about the directory from which the page is served, hence the links may be broken. Instead, use sendRedirect().
Note: to get a request dispatcher object:
- public RequestDispatcher ServletRequest.getRequestDispatcher(String path) - path may be relative, and cannot extend outside current servlet context
- public RequestDispatcher ServletContext.getNamedDispatcher(String name) - name is the registered servlet name in web.xml file
- public RequestDispatcher ServletContext.getRequestDispatcher(String path) - accepts only absolute paths, and not relative paths
4.3 Identify the method used for the following:
Write a message to the WebApp log; Write a message and an exception to the WebApp log.
writing a message to the Web App log:
- void log(String msg) - Writes the specified message to a servlet log file,
usually an event log.
- void log(String message, java.lang.Throwable throwable) - Writes an
explanatory message and a stack trace for a given Throwable exception to the
servlet log file.
- these methods are available in GenericServlet and ServletContext.
writing a message and an exception to the Web App log:
- public void GenericServlet.log(String msg, Throwable t)
- writes the given message and the Throwable's stack trace to a servlet log;
exact output format and location of log are server specific
Return to top