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. Ubuntu搭建NFS服务器,NFS协议详细分析

    目录 1. Ubuntu搭建NFS服务器 2. NFS协议分析 2.1 实验拓扑: 2.2 在kali抓包分析 1. Ubuntu搭建NFS服务器 ​ NFS(Network FileSystem,网 ...

  2. The log scan number (620023:3702:1) passed to log scan in database 'xxxx' is not valid

    昨天一台SQL Server 2008R2的数据库在凌晨5点多抛出下面告警信息: The log scan number (620023:3702:1) passed to log scan in d ...

  3. MySQL mysqlbinlog解析出的SQL语句被注释是怎么回事

    MySQL mysqlbinlog解析出的SQL语句被注释是怎么回事   一网友反馈使用mysqlbinlog解析出的二进制日志中的内容中,有些SQL语句有#注释的情况,这个是怎么回事呢?我们通过实验 ...

  4. 剑指Offer 答题截图

  5. python3基础知识梳理

    一.数据类型 1.数字 int(整型) long(长整型) float(浮点型) complex(复数) 2.布尔值 True或False 3.字符串   二.变量 变量命名规则: 变量名只能是 字母 ...

  6. AI学习---TensorFlow框架介绍[图+会话+张量+变量OP+API]

    TensorFlow的数据流图 TensorFlow的结构分析: 图 + 会话 TensorFlow = 构图阶段(数据与操作的执行步骤被描绘出一个图) + 执行图阶段(使用回话执行构建好的图中操作) ...

  7. Dockfile制作镜像

    讲一个简单的案例 @哈希码用来校验,这样子会安全 MAINTANIER可能将会被LABEL代替,仅仅说说明一下镜像信息罢了. 1.首先是我们创建一个镜像 [root@ELK-chaofeng08 ~] ...

  8. Teradata的profile使用

    1.proflie优势 使用profile可以批量管理用户参数,尤其是在一批用户具有相同的参数配置时,十分便捷. 2.profile可配置用户参数 [Account id][Default datab ...

  9. 【CQOI2014】危桥

    [CQOI2014]危桥 Description Alice和Bob居住在一个由N个岛屿组成的国家,岛屿被编号为\(0\)到\(N-1\).某些岛屿之间有桥相连,桥上的道路都是双向的,但是一次只能供一 ...

  10. 解决 双显卡 y7000笔记本 (Dual Graphics) Ubuntu 18.04 GDM3 无法外接显示器

    sudo gedit /lib/systemd/system/gdm3.service 把其中的 ExecStartPre=/usr/share/gdm/generate-config 更改为 Exe ...