日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OSGI嵌入jetty应用服务器

發布時間:2024/1/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

?

轉載于:https://www.cnblogs.com/dyh004/p/10642407.html

總結

以上是生活随笔為你收集整理的OSGI嵌入jetty应用服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。