OSGI嵌入jetty应用服务器
1、搭建osgi基础环境,参考:https://www.cnblogs.com/dyh004/p/10642383.html
2、引入jetty相关的依赖包
修改jetty启动端口
3、com.kszsa.osgi.hello这个bundle中,引入相关的依赖
4、准备静态页面
jetty.html内容如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jetty说明</title> </head> <body> <h2>这是jetty使用说明</h2> <font color="green">//用来注册诸如表态页面等等</font><br> registerResources(String alias, String name, HttpContext context) <br><br> <font color="green">//用来注册servlet类</font><br> registerServlet(String alias, Servlet servlet, Dictionary initparams, HttpContext context) </body> </html>
5、注册静态资源,修改Activator.java
package com.kszsa.osgi.hello; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpContext; import org.osgi.service.http.HttpService; public class Activator implements BundleActivator { private static BundleContext context; private HttpService service; static BundleContext getContext() { return context; } /** * 启动bundle */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; ServiceReference serviceReference = bundleContext .getServiceReference(HttpService.class.getName()); service = (HttpService) bundleContext.getService(serviceReference); // 注册 HttpContext httpContext = service.createDefaultHttpContext(); // 用来注册诸如表态页面等等 // 设置别名,所有对"/osgi"映射到"web"目录 service.registerResources("/osgi", "/webpage", httpContext); } /** * 停止bundle */ public void stop(BundleContext bundleContext) throws Exception { service.unregister("/osgi"); Activator.context = null; } }
6、启动osgi项目,查看结果,访问http://127.0.0.1:8090/osgi/jetty.html
说明静态资源访问成功。
7、注册servlet资源,新建servlet
package com.kszsa.osgi.servlet; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.framework.BundleContext; public class PrintNameServlet extends HttpServlet{ private static final long serialVersionUID = -9080875068147052401L; private BundleContext context; public PrintNameServlet(BundleContext context) { super(); this.context = context; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); String name = req.getParameter("name"); System.out.println(name); String s = "Hello,world!"; StringBuilder sb = new StringBuilder(); sb.append("<html><title>Response</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); sb.append("<body>"); sb.append(s); sb.append("</body></html>"); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream(),"UTF-8")); bw.write(sb.toString()); bw.flush(); bw.close(); } }
8、修改修改Activator.java,注册servlet
package com.kszsa.osgi.hello; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpContext; import org.osgi.service.http.HttpService; import com.kszsa.osgi.servlet.PrintNameServlet; public class Activator implements BundleActivator { private static BundleContext context; private HttpService service; static BundleContext getContext() { return context; } /** * 启动bundle */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; ServiceReference serviceReference = bundleContext .getServiceReference(HttpService.class.getName()); service = (HttpService) bundleContext.getService(serviceReference); // 注册 HttpContext httpContext = service.createDefaultHttpContext(); // 用来注册诸如表态页面等等 // 设置别名,所有对"/osgi"映射到"web"目录 service.registerResources("/osgi", "/webpage", httpContext); // 注册servlet // 设置servlet别名,'/osgi/print"映射到servlet的实现 service.registerServlet("/osgi/print", new PrintNameServlet( bundleContext), null, httpContext); } /** * 停止bundle */ public void stop(BundleContext bundleContext) throws Exception { service.unregister("/osgi"); Activator.context = null; } }
9、重启osgi,访问http://127.0.0.1:8090/osgi/print
参考地址:https://liugang594.iteye.com/blog/1328050