Home | E-mail 

Exam Notes for the Sun Certified Web Component Developer (SCWCD) Exam

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

4.2. Given a set of business logic exceptions, identify the following:

4.2.1. configuring deployment descriptor for error handling
4.2.1.1. <web-app> …
<error-page>
<error-code>404</ error-code>
<location> /404.html </location>
</error-page> …
</web-app>
4.2.1.2. this specifies that any call to sendError(), from within this web app, with 404 error code should display /404.html; this includes requests for static pages that result in 404 error code
4.2.1.3. the value of location must begin with '/', is treated as based in the context root, and must refer to a resource within the context
4.2.1.4. <location> may be dynamic (e.g. jsp, servlet); for these, the server makes available the following request attributes: javax.servlet.error.status_code and javax.servlet.error.message
4.2.2. configuring deployment descriptor for exception handling
4.2.2.1. <web-app> …
<error-page>
<exception-type> javax.servlet.ServletException</ exception-type >
<location> /servlet/ErrorDisplay </location>
</error-page> …
</web-app>
4.2.2.2. how the server handles exceptions thrown by a servlet is server-dependent, unless an <error-page> entry exists for a specific exception type or a superclass
4.2.2.3. <location> may be dynamic (e.g. jsp, servlet); for these, the server makes available the following request attributes: javax.servlet.error.exception_type & javax.servlet.error.message; the exception object itself is not made available; hence no way to get a stack trace
4.2.2.4. servlets must catch all exceptions except those that subclass ServletException, IOException and RuntimeException (IOException may be caused by client closing the socket by exiting the browser)
4.2.2.5. a ServletException may be created with a message and a "root cause", both optional, e.g. { throw new ServletException("execution interrupted", InterruptedException); }
4.2.2.6. public Throwable ServletException.getRootCause() returns the root cause exception
4.2.2.7. javax.servlet package also defines a subclass of ServletException called UnavailableException(String msg[, int seconds]), which causes server to take servlet out of service
4.2.3. using RequestDispatcher to forward to an error page: see section 1.6 above

4.3. Identify the method used for the following:

4.3.1. writing a message to the Web App log:
4.3.1.1. void log(String msg) - Writes the specified message to a servlet log file, usually an event log.
4.3.1.2. 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.
4.3.1.3. these are methods are available in GenericServlet and ServletContext
4.3.2. writing a message and an exception to the Web App log:
4.3.2.1. public void GenericServlet.log(String msg, Throwable t)
4.3.2.2. writes the given message and the Throwable's stack trace to a servlet log; exact output format and location of log are server specific

Java SCJP 6 Exam Download
Java SCJP 6 Exam Details

Previous                                                   Contents                                                         Next 

Privacy Policy Legal Contact webmaster Web design by anandsoft