1. OSGi Bundle 5. HttpService import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; @Component(service = HttpServlet.class, property = { "alias=/hello" }) public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello, OSGi Servlet!"); } public void activate(HttpService httpService) { try { httpService.registerServlet("/hello", this, null, null); e.printStackTrace(); } } public void deactivate(HttpService httpService) { httpService.unregister("/hello"); } } import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpService; public class Activator implements BundleActivator { private ServiceReference<HttpService> httpServiceReference; @Override public void start(BundleContext context) throws Exception { httpServiceReference = context.getServiceReference(HttpService.class); HttpService httpService = context.getService(httpServiceReference); HelloServlet helloServlet = new HelloServlet(); helloServlet.activate(httpService); } @Override public void stop(BundleContext context) throws Exception { HelloServlet helloServlet = new HelloServlet(); helloServlet.deactivate(context.getService(httpServiceReference)); context.ungetService(httpServiceReference); } }


上一篇:
下一篇:
切换中文