Chapter 9. Error Handling and Debugging 115 Figure 9-4. Debug output Because the debug parameter specifies both resp and stdout, you also get all the debug information in the window in which you started Tomcat. 9.3 Dealing with Runtime Errors Eventually, your application will work the way you want. But things can still go wrong due to problems with external systems your application depends on, such as a database. And even though you have tested and debugged your application, there may be runtime conditions you didn't anticipate. Well-behaved components, such as beans and JSP actions, deal with expected error conditions in a graceful manner. For instance, the UserInfo bean used in Chapter 8 has a valid attribute that is false unless all properties are set to valid values. Your JSP page can then test the property value and present the user with an appropriate message. The JSTL actions also act gracefully in most situations, for instance the <c:forEach> action simply does nothing if the items attribute value is null. Some problems are impossible for the component to handle gracefully, however, and the user needs to be told about the problem instead. The standard way Java does this is to throw an exception. Beans, JSP actions, and the EL processor, can throw exceptions when something goes really bad. By default, the JSP container catches the exception and displays its message and stack trace in the browser, similar to what's shown in Figure 9-1. But that's hardly the type of error message you want the application users to see. Besides, the exception messages may reveal information that can be sensitive from a security point of view, such as file paths and SQL statements. You can present a much more user-friendly, and secure, response by telling the JSP container to use a customized error page instead. Example 9-11 shows a JSP page with a page directive that defines an error page. Chapter 9. Error Handling and Debugging 116 Example 9-11. Page with an error page definition (calc.jsp) <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ page errorPage="errorpage.jsp?debug=log" %> <c:set var="sourcePage" scope="request" value="${pageContext.request.requestURI}" /> <html> <head> <title>Calculator</title> </head> <body bgcolor="white"> <jsp:useBean id="calc" class="com.ora.jsp.beans.calc.CalcBean"> <jsp:setProperty name="calc" property="*" /> </jsp:useBean> <% Calculate the new numbers and state info %> <c:set var="currentNumber" value="${calc.currentNumber}" /> <form action="calc.jsp" method="post"> <table border=1> <tr> <td colspan="4" align="right"> <c:choose> <c:when test="${currentNumber == ''}"> </c:when> <c:otherwise> <c:out value="${currentNumber}" /> </c:otherwise> </c:choose> <input type="hidden" name="currentNumber" value="<c:out value="${currentNumber}" />"> <input type="hidden" name="previousNumber" value="<c:out value="${calc.previousNumber}" />"> <input type="hidden" name="currentOperation" value="<c:out value="${calc.currentOperation}" />"> <input type="hidden" name="reset" value="<c:out value="${calc.reset}" />"> </td> </tr> <tr> <td><input type="submit" name="digit" value=" 7 "></td> <td><input type="submit" name="digit" value=" 8 "></td> <td><input type="submit" name="digit" value=" 9 "></td> <td><input type="submit" name="oper" value=" / "></td> </tr> <tr> <td><input type="submit" name="digit" value=" 4 "></td> <td><input type="submit" name="digit" value=" 5 "></td> <td><input type="submit" name="digit" value=" 6 "></td> <td><input type="submit" name="oper" value=" * "></td> </tr> <tr> <td><input type="submit" name="digit" value=" 1 "></td> <td><input type="submit" name="digit" value=" 2 "></td> <td><input type="submit" name="digit" value=" 3 "></td> <td><input type="submit" name="oper" value=" - "></td> </tr> <tr> <td><input type="submit" name="digit" value=" 0 "></td> <td> </td> <td><input type="submit" name="dot" value=" . "></td> <td><input type="submit" name="oper" value=" + "></td> </tr> . and stdout, you also get all the debug information in the window in which you started Tomcat. 9 .3 Dealing with Runtime Errors Eventually, your application will work the way you want. But things. gracefully, however, and the user needs to be told about the problem instead. The standard way Java does this is to throw an exception. Beans, JSP actions, and the EL processor, can throw exceptions. page contentType="text/html" %> <%@ taglib prefix="c" uri="http:/ /java. sun.com/jstl/core" %> <%@ page errorPage="errorpage.jsp?debug=log" %>