background image

Handling Servlet Errors

<< Specifying Event Listener Classes | Controlling Concurrent Access to Shared Resources >>
<< Specifying Event Listener Classes | Controlling Concurrent Access to Shared Resources >>

Handling Servlet Errors

You can specify an event listener using the deployment descriptor editor of NetBeans IDE by
doing the following:
1. Expand your application's project node.
2. Expand the project's Web Pages and WEB-INF nodes.
3. Double-click web.xml.
4. Click General at the top of the web.xml editor.
5. Expand the Web Application Listeners node.
6. Click Add.
7. In the Add Listener dialog, click Browse to locate the listener class.
8. Click OK.
Handling Servlet Errors
Any number of exceptions can occur when a servlet executes. When an exception occurs, the
web container generates a default page containing the message
A Servlet Exception Has Occurred
But you can also specify that the container should return a specific error page for a given
exception. Review the deployment descriptor file included with the example to learn how to
map the exceptions exception.BookNotFound, exception.BooksNotFound, and
exception.OrderException
returned by the Duke's Bookstore application to errorpage.html.
See
"Mapping Errors to Error Screens" on page 93
for instructions on how to specify error pages
using NetBeans IDE.
Sharing Information
Web components, like most objects, usually work with other objects to accomplish their tasks.
There are several ways they can do this. They can use private helper objects (for example,
JavaBeans components), they can share objects that are attributes of a public scope, they can use
a database, and they can invoke other web resources. The Java Servlet technology mechanisms
that allow a web component to invoke other web resources are described in
"Invoking Other
Web Resources" on page 122
.
Using Scope Objects
Collaborating web components share information by means of objects that are maintained as
attributes of four scope objects. You access these attributes using the [get|set]Attribute
methods of the class representing the scope.
Table 4­3
lists the scope objects.
Sharing Information
Chapter 4 · Java Servlet Technology
105