background image

Declaring Resource References

<< Mapping Errors to Error Screens | The @Resource Annotation >>
<< Mapping Errors to Error Screens | The @Resource Annotation >>

Declaring Resource References

Alternatively, you can click XML at the top of the editor pane and enter the error page mapping
by hand using the following elements:
An exception-type element specifying either the exception or the HTTP status code that
will cause the error page to be opened.
A location element that specifies the name of a web resource to be invoked when the status
code or exception is returned. The name should have a leading forward slash (/).
An error-page element that encloses the previous two elements.
You can have multiple error-page elements in your deployment descriptor. Each one of the
elements identifies a different error that causes an error page to open. This error page can be the
same for any number of error-page elements.
Note ­
You can also define error screens for a JSP page contained in a WAR. If error screens are
defined for both the WAR and a JSP page, the JSP page's error page takes precedence. See
"Handling JSP Page Errors" on page 143
.
For a sample error page mapping, see the example discussed in
"The Example Servlets" on
page 100
.
Declaring Resource References
If your web component uses objects such as enterprise beans, data sources, or web services, you
use Java EE annotations to inject these resources into your application. Annotations eliminate a
lot of the boilerplate lookup code and configuration elements that previous versions of Java EE
required.
Although resource injection using annotations can be more convenient for the developer, there
are some restrictions from using it in web applications. First, you can only inject resources into
container-managed objects. This is because a container must have control over the creation of a
component so that it can perform the injection into a component. As a result, you cannot inject
resources into objects such as simple JavaBeans components. However, JavaServer Faces
managed beans are managed by the container; therefore, they can accept resource injections.
Additionally, JSP pages cannot accept resource injections. This is because the information
represented by annotations must be available at deployment time, but the JSP page is compiled
after that; therefore, the annotation will not be seen when it is needed. Those components that
can accept resource injections are listed in
Table 3­1
.
This section describes how to use a couple of the annotations supported by a servlet container to
inject resources.
Chapter 25, "Persistence in the Web Tier"
describes how web applications use
annotations supported by the Java Persistence API.
Chapter 30, "Securing Web Applications"
describes how to use annotations to specify information about securing web applications.
Configuring Web Applications
The Java EE 5 Tutorial · September 2007
94