Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Exception Handling in JSP

 


[Help | API Docs | Short Course| Exercises]

This exercise implements a JSP page (errhandler.jsp), which processes a POST operation and throws an exception in case of an "incorrect" answer. You will see how these exceptions can be automatically forwarded by the JSP engine to an "error handler." You also develop an error processing JSP page (errorpage.jsp), which receives the exception by means of the exception implicit variable.

Prerequisites

Skeleton Code

Tasks

1. Design a JSP page called errhandler.jsp that can process a POST operation.

2. Indicate an error page, errorpage.jsp, using the page directive for the JSP page.

3. Process the posted form elements. Throw an exception if the value posted for the input element is not equal to an expected value, else print an acknowledgment back to the user.

4. Develop an error page, errorpage.jsp, which can access the runtime exception.

5. Deploy the JSP files for the example within Tomcat.

6. Run the error handling example.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

The following files contain a complete implementation of the JSP error handling example:

Demonstration

From your browser, access the URL http://localhost:8080/examples/jsp/jdc/errHandling/errhandler.jsp

You should see an HTML form as shown below:

[Exception handling within JSP]

Now, make a selection and submit the form. If you made an incorrect selection, an exception is thrown and the request is forwarded to the error handler page, which extracts and displays the exception:

[Exception handling within JSP]

If you made the right choice, you get an acknowledgment from the JSP page itself:

[Exception handling within JSP]

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.