Java Reference
In-Depth Information
To see this example in action, go into the chapter15/httpservice/ directory of
the topic's companion code; type
ant
to build it and
java
-Dorg.osgi.service.
http.port=8080
-jar
launcher.jar
bundles/
to launch it. Then browse to http://
localhost:8080/hello with a web browser.
The relationship between the servlet and HTTP contexts
The HTTP Service specification specifies that only servlets registered with the same
HttpContext
object are part of the same
ServletContext
. The HTTP Service imple-
mentation creates a
ServletContext
for each unique
HttpContext
object that is
registered. If
null
is passed in, the HTTP Service calls
createDefaultHttp-
Context()
, which puts the registered servlet in a separate
ServletContext
.
PAX WEB SUPPORT
Before leaving this section on the
HTTP
Service, we should also point out the support
provided by the Pax Web project (
http://wiki.ops4j.org/display/paxweb/Pax+Web
)
.
This project defines a
WebContainer
service interface that extends the standard
OSG
i
HttpService
interface. This new interface provides a number of extra methods to reg-
ister other servlet-related services, including
JSP
, servlet filters, and servlet event listeners.
We won't cover Pax Web in depth, but we'll show you how to run a shopping cart
example from another Manning publication,
Web Development with Java Server Pages, Sec-
ond Edition
(Fields, Kolb, and Bayern, 2001), in an
OSG
i context. The following listing
shows a Declarative Services component for registering
JSP
s when the
WebContainer
service is published in the
OSG
i service registry.
Listing 15.7 Binder to register JPS pages in the Pax Web
WebContainer
public class Binder {
private volatile HttpContext http;
protected void bindWebContainer(WebContainer c) {
http = c.createDefaultHttpContext();
c.registerJsps(new String[] { "*.jsp" }, http);
}
protected void unbindWebContainer(WebContainer c) {
c.unregisterJsps(http);
http = null;
}
}
This component registers all
JSP
s in the bundle under a shared
HttpContext
and
unregisters the
JSP
s. The next listing shows the Declarative Services component
description.
Listing 15.8 Declarative Services component definition for JSP binder
<?xml version="1.0" encoding="UTF-8"?>
<component name="sample.component" immediate="true">
<implementation class="org.foo.webapp.jspapp.Binder" />