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. java防止double和float精度丢失的方法

    在浮点数当中做运算时经常会出现精度丢失的情况,如果做项目不作处理的话会对商家造成很大的影响的.项目尤其是金融相关的项目对这些运算的精度要求较高. 问题原因:首先计算机进行的是二进制运算,我们输入的十进 ...

  2. hive笔记:时间格式的统一

    一.string类型,年月日部分包含的时间统一格式: 原数据格式(时间字段为string类型) 取数时间和格式的语法  2018-11-01 00:12:49.0 substr(regexp_repl ...

  3. RHEL/Centos7 安装图形化桌面

    Linux是一个多任务的多用户的操作系统,好多linux爱好者在安装完linux后经常遇到一个问题——没有图形化桌面.今天小编在安装RHEL7的时候,一步留神没有安装图形化桌面,下面分享一下安装图形化 ...

  4. 一、Selenium 工作原理

    1.Selenium介绍 Selenium是用于测试Web应用程序用户界面UI的常用框架.端对端的功能测试.并且在一个多个浏览器中操作. 目前Seienium 组件主要包括Selenium IDE   ...

  5. python-turtle 快给你的爷爷看看啥是 “小猪佩奇”

    完整代码: #!/usr/bin/env python2 # coding=utf-8 import turtle t = turtle.Pen() t.pensize(4) t.hideturtle ...

  6. CSS多行文本垂直居中

    今天需要将文本垂直居中,就是一行是垂直居中,多行也是垂直居中. 效果如下 实现代码(同事提供) <!DOCTYPE html> <html> <head lang=&qu ...

  7. 一个tomcat部署多个应用实例

    安装JDK7sudo apt-get install java7-jdk 安装tomcat7 Tomcat7下载地址http://mirror.bjtu.edu.cn/apache/tomcat/to ...

  8. 转:Java中的String,StringBuilder,StringBuffer三者的区别

    最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...

  9. 第一次使用Open Live Writer维护BlogJava

    换了电脑,又重装了一堆东西,现在才把Open Live Writer整好.顺便记下几个心得: Open Live Writer已经没办法从网站上下载了,介绍个方法,可以把地址直接拷贝到迅雷里面,然后请 ...

  10. 新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo

    新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo ...