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

OSGI嵌入jetty应用服务器的更多相关文章

  1. OSGI嵌入tomcat应用服务器(gem-web)——tomcat插件环境搭建

    相关的资源下载,参考:https://www.cnblogs.com/dyh004/p/10642769.html 新建普通的plugin工程 新建工程运行环境 在工程中,新建运行环境 新建存放运行环 ...

  2. OSGI嵌入tomcat应用服务器(gem-web)——资源下载

    Gem-Web官网介绍: 官网地址:https://www.eclipse.org/gemini/web/download/milestones.php 1.1. 官方正式发布版 https://ww ...

  3. Jetty应用服务器的安装详解

    Jetty是一个开源的Servlet容器和应用服务器,它极度轻量级.高便携性.功能强大.灵活和扩展性好,而且支持各种技术如SPDY.WebSocket.OSGi.JMX.JNDI和JAAS.Jetty ...

  4. web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)

    在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序. 由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项 ...

  5. eclipse 项目中嵌入jetty

    Jetty是一个提供HHTP服务器.HTTP客户端和javax.servlet容器的开源项目,Jetty 目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的 ...

  6. 润乾在jetty应用服务器下的JNDI配置一

     一. 此处绑定的数据源是以 DBCP 为实现.首先必须将数据库驱动(这里用了MYSQL数据库)和DBCP所需要的 Jar 包复制到 Jetty 根目录的 lib 目录下.DBCP主要需要以下3个 ...

  7. 嵌入jetty到Java代码

    在做Demo实例时,使用的jetty版本号为8.x. 为了避免麻烦,将全部的包都导入到MyEclipse的lib文件夹下. 实例1:自己定义handler的服务器 package com.jetty. ...

  8. 将jetty嵌入到应用中的简单案例

    前面说过jetty最广泛的应用是可以方便的嵌入到应用程序中,而不是作为应用服务器,下面就用最简单的demo来演示一个最简单的应用 1.下载并导入依赖 首先应该建立一个普通的java项目,然后把依赖包导 ...

  9. Jetty 9嵌入式开发

    官方网址:http://www.eclipse.org/jetty/ 下载地址:http://download.eclipse.org/jetty/stable-9/dist/ 文档网址:http:/ ...

随机推荐

  1. zabbix监控自动发现监控tomcat(V1)

    背景说明: 由于zabbix监控使用自带的模版,只能监控主机上只有1个tomcat的场景适合,虽然网上很多朋友都是在每个监控项上面添加一个空格来解决问题.但是个人感觉这种方法还是蛮麻烦的,所以写一篇使 ...

  2. SQL SERVER 查看占用tempDB

    use tempdb go t1.session_id, t1.internal_objects_alloc_page_count, t1.user_objects_alloc_page_count, ...

  3. 转: OVER() 系列函数介绍

    OVER(PARTITION BY)函数介绍 开窗函数               Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...

  4. PostgreSQL远程访问设置

    数据库版本:9.3.23(Windows xp系统) 步骤: 1.需要修改数据库安装目录下的pg_hba.conf文件 修改成: 2.并使用psql执行pg_ctl reload重新加载配置文件

  5. XSS 漏洞原理及防御方法

    XSS跨站脚本攻击:两种情况.一种通过外部输入然后直接在浏览器端触发,即反射型XSS:还有一种则是先把利用代码保存在数据库或文件中,当web程序读取利用代码并输出在页面上时触发漏洞,即存储型XSS.D ...

  6. Linux 小知识翻译 - 「syslog」

    这次聊聊「syslog」. 上次聊了「日志」(lgo).这次说起syslog,一看到log(日志)就明白是怎么回事了.syslog是获取系统日志的工具. 很多UINIX系的OS都采用了这个程序,它承担 ...

  7. Zookeeper运维小结--CancelledKeyException

    https://www.jianshu.com/p/73eec030db86 项目中用到storm+kafka+zookeeper,在实际应用中zk和kafka常出问题,这里记录下在使用zk过程中的问 ...

  8. 轮播图插件swiper 的使用

    引入文件(注:目前版本号为Swiper3.x) <link rel="stylesheet" type="text/css" href="//s ...

  9. 面试----你可以手写一个promise吗

    参考:https://www.jianshu.com/p/473cd754311f <!DOCTYPE html> <html> <head> <meta c ...

  10. 解决新版chrome无法手动拖动安装插件 提示“无法从该网站添加应用,扩展程序和用户脚本”

    开发模式安装 把下载后的.crx扩展名的离线Chrome插件的文件扩展名改成.zip或者.rar 解压压缩文件 在Chrome的地址栏中输入:chrome://extensions/ 打开Chrome ...