How to make servlet using OSGi

hello everyone..
Here I'll tell you how to make  Servlet  using Osgi framework.
1. Make new plug-ins project give the name as SimpleServlet.
 Make sure to select Equinox as the target OSGI framework on the first page of the wizard. Also make sure you’ve got an activator (that option is on by default).
 In menifest Eddition -> Dependencies.
import these packages.

* javax.servlet

* javax.servlet.http

* org.osgi.framework

* org.osgi.service.http

* org.osgi.util.tracker

2.make a new class (SimpleServlet)

package simpleservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet  extends HttpServlet{
    private static final long serialVersionUID = 1L;

      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        resp.getWriter().write("Hello world!!");

3.Now make a 2nd  new class for Servlet Service Tracker. (HttpServiceTracker)

package simpleservlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;
public class HttpServiceTracker  extends ServiceTracker{
      public HttpServiceTracker(BundleContext context) {
          super(context, HttpService.class.getName(), null);
        // TODO Auto-generated constructor stub

    public Object addingService(ServiceReference reference) {
        HttpService httpService = (HttpService) super.addingService(reference);
        if (httpService == null)
          return null;

        try {
          System.out.println("Registering servlet at /simple");
          httpService.registerServlet("/simple", new SimpleServlet(), null, null);
        } catch (Exception e) {

        return httpService;

      public void removedService(ServiceReference reference, Object service) {
        HttpService httpService = (HttpService) service;

        System.out.println("Unregistering /simple");

        super.removedService(reference, service);


4. Now Come to activator. which is main part of the OSGi.
Edit your Activator like This-
package simpleservlet;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {
    private HttpServiceTracker serviceTracker;
    private static BundleContext context;

    static BundleContext getContext() {
        return context;

     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        serviceTracker = new HttpServiceTracker(context);

     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;
         serviceTracker = null;

} configuration..
Make a New Osgi framework run. then go to tab  arguments -> VM arguments write another line

now Apply and run.
5. go to your chrome and type
it will print "Hello world"
 and tutorial for this artical  is available at
Thank you!!

                                                                                                    Anshul Madan
                                                                         CASCADEMIC Solutins Pvt.Ltd


Popular posts from this blog

LoRa and its suitability for IoT

How to start With OSGi

CASCADEMIC becomes MICROCHIP's Authorised Design Partner