如何在osgi中使用servlet [英] How to use servlet with osgi

查看:287
本文介绍了如何在osgi中使用servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建并部署一个Web服务到OSGi容器。例如,将服务发布到地址:

  http:// localhost:8080 / testservice。 

该服务在servlet中生成HTML响应。



  public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest请求,HttpServletResponse响应)throws ServletException,IOException {
response.setContentType(text / html);
PrintWriter out = response.getWriter();

out.println(< html>);
out.println(< head>);
out.println(< title> Hola< / title>);
out.println(< / head>);
out.println(< body bgcolor = \white \>);
out.println(< / body>);
out.println(< / html>);
}

}

我需要使用的工具:
$ b


  1. maven创建项目


  2. 保存ESB karaf作为OSGi容器




  3. 问题是我不知道如何使用Maven创建和实现这样的Web服务,如:

    如何指定webapp / web.xml



    如何指定pom.xml:依赖项,包类型,插件

    如何注册service:实现BundlActivator或配置spring xml文件

    任何人都可以帮助我吗?如果您使用bndtools,创建一个Declarative Services项目并将此注释添加到您的Servlet中:

      @Component(provide = Servlet.class,properties = {alias = / hello})
    public class HelloWorldServlet扩展HttpServlet {...}

    然后用'Apache Felix 4创建一个bnd运行描述符Web控制台和Gogo',只需添加Apache Felix Http白板捆绑软件即可。你可以在 http:// localhost:8080 / hello



    找到你的servlet。它是如何工作的。 @Component注释使您的类成为服务(在这种情况下,由于提供属性,Servlet服务)。这是用服务属性别名注册的。 Apache Felix Http Whiteboard软件包提取这些服务并将它们注册为servlet。我认为它不会比这更简单。


    I want to create and deploy a web service to OSGi container. For example, publish the service to the address:

    http://localhost:8080/testservice. 
    

    The service generate HTML response in a servlet.

    I have searched a lot and got:

    public class HelloWorldServlet extends HttpServlet {
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("text/html");
       PrintWriter out = response.getWriter();
    
       out.println("<html>");
       out.println("<head>");
       out.println("<title>Hola</title>");
       out.println("</head>");
       out.println("<body bgcolor=\"white\">");
       out.println("</body>");
       out.println("</html>");
    }
    

    }

    The tool I need to use:

    1. maven to create the project

    2. Fuse ESB karaf as OSGi container

    The question is that I do not know how to use Maven to create and implement such web service, like:

    how to specify webapp/web.xml

    how to specify pom.xml: dependencies, package type, plugin

    how to register service: implement BundlActivator or configure spring xml file

    Can anyone help me with this? Is there a detailed tutorial for newbie?

    解决方案

    If you use bndtools, create a Declarative Services project and add this annotation to your servlet:

     @Component(provide = Servlet.class, properties = {"alias=/hello"})
     public class HelloWorldServlet extends HttpServlet { ... }
    

    Then create a bnd run descriptor with 'Apache Felix 4 with Web Console and Gogo', just add the Apache Felix Http whiteboard bundle and you're good to go. You can find your servlet at http://localhost:8080/hello

    How it works. The @Component annotation makes your class a service (a Servlet service in this case due to the provide attribute). This is registered with the service property 'alias'. The Apache Felix Http Whiteboard bundle picks up these services and registers them as servlets. I do not think it can get any simpler than this.

    这篇关于如何在osgi中使用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆