If you've taken a look at J2EE and JavaServer Pages technology, and want to examine JSP more closely, you've come to the right place. This article is the second of two parts, excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413).
JavaServer Pages, conclusion - The Deployment Descriptor (Page 5 of 7 )
The deployment descriptor allows you to specify application-wide error handlers for errors in the application. This provides a way to specify different error pages for exceptions that might occur within a single page. If a given exception or HTML error occurs anywhere in the application, the deployment descriptor identifies an error page that can be served to the client. Of course, a specific error page identified in a JSP page takes precedence over the error page identified in the deployment descriptor.
You can specify error pages for Java exceptions, and error pages for HTML errors. Error page elements come immediately after the <welcome-file-list> element in the deployment descriptor.
To specify an error page for a Java exception, use this element in the deployment descriptor:
Add this page to the JavaFAQ application. This page is Threading.jsp, and it is located in the root directory of the application (the same directory in which welcome.jsp is located):
<%@ page errorPage="/WEB-INF/errorPage.jsp" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head><title>Threading FAQs</title></head> <body> <% Integer i = new Integer("string"); %> </body> </html>
Modify errorPage.jsp as shown here:
<%@ page isErrorPage="true" import="java.io.PrintWriter" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Error</title> </head> <body> <h1> Error</h1> There was an error somewhere. <p>Here is the stack trace <p><% exception.printStackTrace(new PrintWriter(out)); %> <%@ include file="/WEB-INF/footer.jspf" %> </body> </html>
Create the JSP Dates_and_Times.jsp in the root directory:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Dates and Times FAQ</title> </head> <body> <h1> Dates and Times FAQ</h1> <% Integer i = new Integer("string"); %> <%@ include file="/WEB-INF/footer.jspf"%> </body> </html>
Create two HTML pages that will be used as error pages. The first is NoSuchPage.html, and it is located in the WEB-INF directory:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Resource Not Found</title> </head> <body> <h1> Resource Not Found</h1> You are attempting to go to a page that does not exist or is not available. If you entered the address by hand, please go to the <a href="welcome.jsp">Welcome Page</a>. <p>If you clicked on a link on this site, the page is temporarily unavailable. Try again later. <%@ include file="/WEB-INF/footer.jspf"%> </body> </html>
The second error page is BadNumber.html. It too is located in the WEB-INF directory:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Invalid Number</title> </head> <body> <h1> Invalid Number</h1> You entered a number that is incorrect. Only digits are allowed. Please press the back button and try again. <%@ include file="/WEB-INF/footer.jspf"%> </body> </html>
Modify the deployment descriptor. If you are using J2EE, go to step 8. If you are using Tomcat, edit the web.xml file as shown here:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <!-- this is the deployment descriptor for Chapter 3 Try It Out example 3 --> <welcome-file-list> <welcome-file>welcome.jsp</welcome-file> </welcome-file-list> <error-page> <exception-type>java.lang.NumberFormatException</exception-type> <location>/WEB-INF/BadNumber.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF /NoSuchPage.html</location> </error-page> </web-app>
If you are using the J2EE Deployment Tool, modify the File Refs tab for the WebApp as shown below. You need to add two entries to the Error Mapping list. Add the same mappings that are shown in the web.xml file in step 4:
Deploy the application.
Open a browser and navigate through the screens until you reach the topic list page as shown below:
Click the link for Threading. The browser should look something like this:
Click the link for Dates and Times. You will see this:
13. Click the link for Strings and StringBuffers. You will see this:
How It Works
Weve added three pages to the application, each of which causes a different error-handling mechanism to control the page flow.
The Threading.jsp page included a page directive that specified the error page. Since Threading.jsp attempts to create an Integer object with an invalid argument to the constructor, an exception is thrown, and the page does not have an error handler to catch the exception. This causes the server to call the error page specified by the page directive, and that page is sent to the client.
The error page, errorPage.jsp, has access to the implicit exception object. This is because the page includes the page directive with the isErrorPage attribute set to true. Pages that dont have this attribute do not have access to the exception object. We can use this object together with the implicit out object to print out the stack trace to the response like this:
This works because the java.lang.Throwable interface defines a printStackTrace(PrintWriter) method. The PrintWriter constructor can take an OutputStream instance, which is exactly the type of the implicit out object. The method prints the stack trace to the given PrintWriter. (Keep in mind that you wouldnt print a stack trace in a live page meant for a user of the application. It provides no useful information for users, and just gives them a bad feeling about your application. The example above is used to show that you can access the implicit exception object in an error page.)
The Date_and_Times.jsp also uses an Integer object to cause an exception to be thrown from the page. However, this page does not specify an error handler in the page directive. In this case, the server matches the exception thrown to an exception specified in an <error-page> element in the deployment descriptor. The server sends the BadNumber.html page to the client. If the exception did not match a specification in the deployment descriptor, the server would probably have sent an HTTP 500 error to the client.
Finally, the Strings_and_StringBuffer.jsp page does not exist. This creates an HTTP 404 error in the server. Since this error code matches an error code specified in an <error-page> element in the deployment descriptor, the server sends the specified page to the client. If the error code had not matched a specification in the deployment descriptor, the server would have taken some server-specific action. Some servers, such as Tomcat, may send a server-specific page back to the client with the error; other servers might simply send the error code to the browser and let the browser decide how to display the error to the user.