OSGi HTTP Service: Registering Servlets on-the-fly
There’s a lot of talk lately about Web application development in OSGi, and there’s even an ongoing standardization effort (RFC 66) trying to define an OSGi web application model in the upcoming R4.2 release.
However, it is worth noting that the OSGi HTTP Service is one of the oldest compendium services; and that, while simplistic and limited, there are many applications using it (we already mentioned the Apache Felix Web Console).
The OSGi HTTP Service provides a simple OSGi service to deploy servlets and resources at runtime; in this article I’ll focus on the first part, implementing and deploying a simple Servlet.
First of all, you will need an OSGi HTTP Service implementation. As usual, Apache Felix provides a framework-independent bundle; so, without further ado, you can download it and install it in your OSGi container, or simply type
$ pax-run.sh http://www.osgilook.com/static/samples/osgi-http-service.txt
(By the way: have a look at our OSGi Demos for other easy, pre-cooked, demos)
Let’s now develop a simple Servlet. You don’t need to make it OSGi dependent in any way, so the usual Hello World Servlet would work:
public class DateServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.getWriter().write("Hi, today is "+ new Date());
}
}
Easy. Now let’s write the usual deployment descript… no way! We want to deploy it on the fly, and without any boring XML descriptor!
What we need is to get access to the OSGi HttpService, and declare the new servlet. We can do that, as usual, in the bundle activator.
public class Activator implements BundleActivator
{
public void start(BundleContext context) throws Exception
{
ServiceReference sRef = context.getServiceReference(HttpService.class.getName());
if (sRef != null)
{
HttpService service = (HttpService) context.getService(sRef);
service.registerServlet("/date", new DateServlet(), null, null);
}
}
public void stop(BundleContext context) throws Exception
{
ServiceReference sRef = context.getServiceReference(HttpService.class.getName());
if (sRef != null)
{
HttpService service = (HttpService) context.getService(sRef);
service.unregister("/date");
}
}
}
Now package your bundle (don’t forget to specify the Bundle-Activator) and deploy it in the running OSGi container. If everything works fine, you’ll be able to reach your servlet at http://localhost:8080/date.
There are a few things the OSGi HTTP Service can also do, like registering resources. I’ll talk about that in the next blog post.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Thanks for the post.
Osgi is becoming more and more interesting.
I’m wondering if jsp will be available on osgi new web specifications or not.
Hi Uberto, using the plain OSGi HTTP Service (such as the one provided by the Felix HTTP Bundle), supporting JSPs is tricky to say the least.
There is however an already working solution in PaxWeb, and the upcoming RFC 66 will support not only JSPs, but also War deployment.
@Uberto, Apache Sling does provide JSP scripting in an OSGi environment, see http://sling.apache.org
thanks for the quick and informative response.