Micro-management of Exceptions involves handling of each exception using the traditional try-catch-finally statements. It is possible to use these statements in JSP as well as Servlet code. Exceptions that are not caught using the above approach can be sent to a separate JSP page.

This approach ensures that uncaught exceptions are never displayed directly to the end user.  A JSP page that is designed to only handle errors is referred to as an error page. A JSP page can be marked as an error page by setting the page directive isErrorPage to true. Marking a page as an error page using the page directive ensures that the implicit exception object is preserved and made available to this error page. The error page can access the exception object and attempt to determine what has gone wrong. In the example shown here, you will see an extract from an error handling JSP page.

 <%@ pageisErrorPage="true" %>           

An error has occurred

        …

…more error handling code andmessages     The mechanism to handle Exceptions in Servlets is somewhat similar to what we have seen for JSPs, but not exactly identical. In Servlets, there is no equivalent way to define an error page using the page directive.

Instead, a servlet that is acting as the error page must retrieve the exception object from the request variable. Let’s look at an example.   publicclass EnhancedErrorPage extends HttpServlet {     …..     // Get the exception passed by JSP   Exception e =(Exception)request.getAttribute(“javax.servlet.jsp.jspException”);   out.println(““);  out.println(““);  out.println(“

An Error HasOccurred : ” + e.getMessage() + “

“);  out.println(““);  out.println(““); }  In this Servlet code, we see that the exception is first retrieved from the request object and then the getMessage method is invoked to get to the actual exception message.

x

Hi!
I'm Erica!

Would you like to get a custom essay? How about receiving a customized one?

Check it out