Java & J2EE Page 4 - The JSP Files (part 7): Bugs, Beans And Banks |
The exception-handling routine "error.jsp" can be as simple or complex as you want to make it. If you want something friendly and easy to understand, you could use the following:
Now, if you run the example above again, JSP should automatically divert you to this page instead of grossing you out with long and icky error messages. The script above simply notifies the user that an error has occurred; it says nothing about the type of error, or the reasons for its occurrence. If you'd like to display this as well, you need to use the built-in Exception object to obtain information about the exception. The next example uses a modified "error.jsp" to illustrate this:
And this time, the output will be a little more helpful.
Pay special attention to the first line of the script; the directive
tells JSP that this is an error page, and initializes an instance of the Exception object for use. The Exception object comes with a couple of useful methods - the exception.getMessage() method is used to obtain a description of the error, while the exception.printStackTrace() method is used to print debugging information.
blog comments powered by Disqus |