Home arrow Java arrow Page 5 - JavaServer Pages, conclusion

JavaServer Pages, conclusion

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).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 17
November 10, 2005
  1. · JavaServer Pages, conclusion
  2. · The config Object
  3. · Try It Out: Using JavaBeans in JSP Pages
  4. · Translation and Compilation
  5. · The Deployment Descriptor
  6. · Including and Forwarding from JSP Pages
  7. · Summary

print this article

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:


To specify an error page for an HTML error, use this element:


A complete list of the HTML error codes can be found in the HTTP specification at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.

Try It OutException Handling in JSP Pages

In this example, we will add error handling to the Java FAQ application.

  1. The structure of the web application looks like this:

  2. 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">
      <head><title>Threading FAQs</title></head>
    <% Integer i = new Integer("string"); %>

  3. Modify errorPage.jsp as shown here:

    <%@ page isErrorPage="true" import="java.io.PrintWriter" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <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" %>
  4. Create the JSP Dates_and_Times.jsp in the root directory:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <title>Dates and Times FAQ</title>
    <h1> Dates and Times FAQ</h1>
    <% Integer i = new Integer("string"); %>
    <%@ include file="/WEB-INF/footer.jspf"%>

  5. 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">
        <title>Resource Not Found</title>
        <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"%>

  6. 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">
        <title>Invalid Number</title>
        <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"%>

  7. 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"
    <!-- this is the deployment descriptor for Chapter 3
           Try It Out example 3                        -->
        <location>/WEB-INF  /NoSuchPage.html</location>
  8. 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:


  9. Deploy the application.


  10. Open a browser and navigate through the screens until you reach the topic list page as shown below: 


  11. Click the link for Threading. The browser should look something like this: 


  12. 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:

  <p><% exception.printStackTrace(new PrintWriter(out)); %>

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.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials