import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; public class MyBundleActivator implements BundleActivator { @Override public void start(BundleContext bundleContext) throws Exception { HttpService httpService = (HttpService) bundleContext.getService( bundleContext.getServiceReference(HttpService.class.getName())); httpService.registerServlet("/hello", new MyServlet(), null, null); } @Override public void stop(BundleContext bundleContext) throws Exception { HttpService httpService = (HttpService) bundleContext.getService( bundleContext.getServiceReference(HttpService.class.getName())); httpService.unregister("/hello"); } } import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.write("Hello, OSGi HTTP!"); writer.close(); } } Bundle-Activator: com.example.MyBundleActivator Import-Package: javax.servlet, org.osgi.service.http; version="1.2" <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_5.xsd" version="2.5"> <display-name>OSGi HTTP Example</display-name> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.example.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>


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