import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class MyHttpService implements BundleActivator {
private HttpService httpService;
public void start(BundleContext bundleContext) {
try {
httpService = bundleContext.getService(bundleContext.getServiceReference(HttpService.class));
httpService.registerServlet("/myServlet", new MyServlet(), null, null);
} catch (NamespaceException e) {
e.printStackTrace();
}
}
public void stop(BundleContext bundleContext) {
httpService.unregister("/myServlet");
httpService = null;
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("Hello, World!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
response.getWriter().println("Hello, " + name + "!");
}
}
Bundle-Name: My HTTP Service
Bundle-SymbolicName: my-http-service
Bundle-Activator: com.example.MyHttpService
Import-Package: org.osgi.framework, org.osgi.service.http