type=page
status=published
title=Configuring Web Applications
next=webapp006.html
prev=webapp004.html
~~~~~~
= Configuring Web Applications


[[CHDHGJIA]]

[[configuring-web-applications]]
Configuring Web Applications
----------------------------

This section describes the following tasks involved with configuring web
applications:

* Setting context parameters
* Declaring welcome files
* Mapping errors to error screens
* Declaring resource references

[[BNAES]]

[[setting-context-parameters]]
Setting Context Parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~

The web components in a web module share an object that represents their
application context. You can pass context parameters to the context, or
you can pass initialization parameters to a servlet. Context parameters
are available to the entire application. For information on
initialization parameters, see link:servlets004.html#BNAFU[Creating and
Initializing a Servlet].

[[GJSFJ]]

[[to-add-a-context-parameter-using-netbeans-ide]]
To Add a Context Parameter Using NetBeans IDE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

These steps apply generally to web applications but do not apply
specifically to the examples in this chapter.

To add a context parameter using NetBeans IDE:

1.  Open the project.
2.  Expand the project's node in the Projects tree.
3.  Expand the Web Pages node and then the WEB-INF node.
4.  Double-click `web.xml`.
+
If the project does not have a `web.xml` file, create one by following
the steps in link:#GKIHH[To Create a web.xml File Using NetBeans IDE].
5.  Click General at the top of the editor window.
6.  Expand the Context Parameters node.
7.  Click Add.
8.  In the Add Context Parameter dialog box, in the Parameter Name
field, enter the name that specifies the context object.
9.  In the Parameter Value field, enter the parameter to pass to the
context object.
10. Click OK.

[[GKIHH]]

[[to-create-a-web.xml-file-using-netbeans-ide]]
To Create a web.xml File Using NetBeans IDE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To create a `web.xml` file using NetBeans IDE:

1.  From the File menu, choose New File.
2.  In the New File wizard, select the Web category, then select
Standard Deployment Descriptor under File Types.
3.  Click Next.
4.  Click Finish.
+
A basic `web.xml` file appears in `web/WEB-INF/`.

[[BNAER]]

[[declaring-welcome-files]]
Declaring Welcome Files
~~~~~~~~~~~~~~~~~~~~~~~

The welcome files mechanism allows you to specify a list of files that
the web container can append to a request for a URL (called a valid
partial request) that is not mapped to a web component. For example,
suppose that you define a welcome file `welcome.html`. When a client
requests a URL such as `_host:port/webapp/directory_`, where `_directory_`
is not mapped to a servlet or XHTML page, the file
`_host:port/webapp/directory/_`welcome.html` is returned to the
client.

If a web container receives a valid partial request, the web container
examines the welcome file list, appends to the partial request each
welcome file in the order specified, and checks whether a static
resource or servlet in the WAR is mapped to that request URL. The web
container then sends the request to the first resource that matches in
the WAR.

If no welcome file is specified, GlassFish Server will use a file named
`index.html` as the default welcome file. If there is no welcome file
and no file named `index.html`, GlassFish Server returns a directory
listing.

You specify welcome files in the `web.xml` file. The welcome file
specification for the `hello1` example looks like this:

[source,oac_no_warn]
----
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
----

A specified welcome file must not have a leading or trailing slash
(`/`).

The `hello2` example does not specify a welcome file, because the URL
request is mapped to the `GreetingServlet` web component through the URL
pattern `/greeting`.

[[GKBKW]]

[[mapping-errors-to-error-screens]]
Mapping Errors to Error Screens
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When an error occurs during execution of a web application, you can have
the application display a specific error screen according to the type of
error. In particular, you can specify a mapping between the status code
returned in an HTTP response or a Java programming language exception
returned by any web component and any type of error screen.

You can have multiple `error-page` elements in your deployment
descriptor. Each element 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.

[[BNAET]]

[[to-set-up-error-mapping-using-netbeans-ide]]
To Set Up Error Mapping Using NetBeans IDE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

These steps apply generally to web applications but do not apply
specifically to the examples in this chapter.

To set up error mapping using NetBeans IDE:

1.  Open the project.
2.  Expand the project's node in the Projects tab.
3.  Expand the Web Pages node and then the WEB-INF node.
4.  Double-click `web.xml`.
+
If the project does not have a `web.xml` file, create one by following
the steps in link:#GKIHH[To Create a web.xml File Using NetBeans IDE].
5.  Click Pages at the top of the editor window.
6.  Expand the Error Pages node.
7.  Click Add.
8.  In the Add Error Page dialog box, click Browse to locate the page
that you want to act as the error page.
9.  Specify either an error code or an exception type.
* To specify an error code, in the Error Code field enter the HTTP
status code that will cause the error page to be opened, or leave the
field blank to include all error codes.
* To specify an exception type, in the Exception Type field enter the
exception that will cause the error page to load. To specify all
throwable errors and exceptions, enter `java.lang.Throwable`.
10. Click OK.

[[BNAEU]]

[[declaring-resource-references]]
Declaring Resource References
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If your web component uses such objects 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 on using it in web
applications. First, you can inject resources only into
container-managed objects, 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 such objects as
simple JavaBeans components. However, managed beans are managed by the
container; therefore, they can accept resource injections.

Components that can accept resource injections are listed in
link:#BNAEV[Table 6-1].

This section explains how to use a couple of the annotations supported
by a web container to inject resources.
link:persistence-basicexamples.html#GIJST[Chapter 41, "Running the
Persistence Examples"], explains how web applications use annotations
supported by the Java Persistence API.
link:security-webtier.html#BNCAS[Chapter 51, "Getting Started Securing
Web Applications"], explains how to use annotations to specify
information about securing web applications. See
link:resources.html#BNCJH[Chapter 55, "Resource Adapters and Contracts"],
for more information on resources.

[[sthref25]][[BNAEV]]

*Table 6-1 Web Components That Accept Resource Injections*

[width="50%",cols="20%,30%"]
|================================================
|*Component* |*Interface/Class*
|Servlets |`javax.servlet.Servlet`
|Servlet filters |`javax.servlet.ServletFilter`
|Event listeners a|
`javax.servlet.ServletContextListener`

`javax.servlet.ServletContextAttributeListener`

`javax.servlet.ServletRequestListener`

`javax.servlet.ServletRequestAttributeListener`

`javax.servlet.http.HttpSessionListener`

`javax.servlet.http.HttpSessionAttributeListener`

`javax.servlet.http.HttpSessionBindingListener`

|Managed beans |Plain Old Java Objects
|================================================


[[BNAEW]]

[[declaring-a-reference-to-a-resource]]
Declaring a Reference to a Resource
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The `@Resource` annotation is used to declare a reference to a resource,
such as a data source, an enterprise bean, or an environment entry.

The `@Resource` annotation is specified on a class, a method, or a
field. The container is responsible for injecting references to
resources declared by the `@Resource` annotation and mapping it to the
proper JNDI resources.

In the following example, the `@Resource` annotation is used to inject a
data source into a component that needs to make a connection to the data
source, as is done when using JDBC technology to access a relational
database:

[source,oac_no_warn]
----
@Resource javax.sql.DataSource catalogDS;
public getProductsByCategory() {
    // get a connection and execute the query
    Connection conn = catalogDS.getConnection();
    ...
}
----

The container injects this data source prior to the component's being
made available to the application. The data source JNDI mapping is
inferred from the field name, `catalogDS`, and the type,
`javax.sql.DataSource`.

If you have multiple resources that you need to inject into one
component, you need to use the `@Resources` annotation to contain them,
as shown by the following example:

[source,oac_no_warn]
----
@Resources ({
    @Resource(name="myDB" type=javax.sql.DataSource.class),
    @Resource(name="myMQ" type=javax.jms.ConnectionFactory.class)
})
----

The web application examples in this tutorial use the Java Persistence
API to access relational databases. This API does not require you to
explicitly create a connection to a data source. Therefore, the examples
do not use the `@Resource` annotation to inject a data source. However,
this API supports the `@PersistenceUnit` and `@PersistenceContext`
annotations for injecting `EntityManagerFactory` and `EntityManager`
instances, respectively.
link:persistence-basicexamples.html#GIJST[Chapter 41, "Running the
Persistence Examples"] describes these annotations and the use of the
Java Persistence API in web applications.

[[BNAEX]]

[[declaring-a-reference-to-a-web-service]]
Declaring a Reference to a Web Service
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The `@WebServiceRef` annotation provides a reference to a web service.
The following example shows uses the `@WebServiceRef` annotation to
declare a reference to a web service. `WebServiceRef` uses the
`wsdlLocation` element to specify the URI of the deployed service's WSDL
file:

[source,oac_no_warn]
----
...
import javax.xml.ws.WebServiceRef;
...
public class ResponseServlet extends HTTPServlet {
@WebServiceRef(wsdlLocation="http://localhost:8080/helloservice/hello?wsdl")
static HelloService service;
----
