Java Reference
In-Depth Information
PrintWriter html = response.getWriter();
html.append("Hello valued customer!<br/>");
try {
InitialContext ctx = new InitialContext();
String jndiName = "osgi:service/" +
CurrentOffers offers =
(CurrentOffers) ctx.lookup(jndiName);
List<SpecialOffer> currentOffers =
for (SpecialOffer offer : currentOffers) {
writeRowForOffer(html, offer);
} catch (NamingException e) {
html.append("We have no special offers today. " +
"Try again tomorrow.");
private void writeRowForOffer(PrintWriter html,
SpecialOffer offer) {
String description = offer.getDescription();
Food offerFood = offer.getOfferFood();
html.append("<td>" + offerFood.getName() + "</td>");
html.append("<td>" + offerFood.getPrice() + "</td>");
html.append("<td>" + description + "</td>");
The osgi:service/ namespace provides access to services in the OSG i Service Registry.
Services are registered under their interface name, which makes them easy to find. If no
services implementing the SpecialOffer interface are registered, a NamingException
will be thrown. In this case that's not a problem—special offers aren't compulsory! (The
sharp-eyed among you will notice that we've abandoned internationalization again to
keep the sample shorter.)
Implementing SayHello JNDI has added some new dependencies, and that needs to
be reflected in the manifest. The fancyfood.offers , , and javax
.naming packages need to be imported:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: fancyfoods.web
Bundle-Version: 1.0.0
Bundle-ClassPath: WEB-INF/classes
Web-ContextPath: /fancyfoods.web
Import-Package: fancyfoods.offers;version="[1.0, 2.0)",
Search WWH ::

Custom Search