Servlets 在OSGi中注册接收参数的servlet

Servlets 在OSGi中注册接收参数的servlet,servlets,osgi,Servlets,Osgi,我正在尝试注册一个HttpServlet,它将接收参数(不管它是通过POST还是GET,尽管POST显然是首选)。基本上只是扩展了这里描述的内容: 在这里: 我没有使用静态声明式注册,首先希望看到它工作,然后我会做其他事情 打电话时会产生疑问: httpService.registerServlet("/helloworld", new RestServlet(), null, null); 不确定如何告诉HttpService服务器将接受参数。此外,每次注册servlet时都必须使用ne

我正在尝试注册一个HttpServlet,它将接收参数(不管它是通过POST还是GET,尽管POST显然是首选)。基本上只是扩展了这里描述的内容:

在这里:

我没有使用静态声明式注册,首先希望看到它工作,然后我会做其他事情

打电话时会产生疑问:

httpService.registerServlet("/helloworld", new RestServlet(), null, null);
不确定如何告诉HttpService服务器将接受参数。此外,每次注册servlet时都必须使用new()创建一个HttpServlet,还是可以将其用于不同的别名?我这样问是因为也许可以在alias参数中使用一些通配符,然后让HttpServlet对象处理HttpRequest中的任何内容

欢迎提供任何帮助/建议/想法

问候,,
Alex

我对OSGI不太了解,但在我看来,它更像是一个纯粹的servlet问题。我看了你提供的链接,希望我能帮助你

首先,我认为您不需要告诉
HttpService
它将接受params。使用servlet时,只需提取请求参数:

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException {
    req.getParameter(paramName); // get a request parameter
}
其次,我认为可以对多个“别名”使用同一个Servlet。对我来说,这似乎是一种servlet映射:可以对同一个servlet使用多个映射(
/helloworld
/helloxyz
等)。

  • 如果忽略servlet init,则可以使用同一个servlet多次注册
  • 如果你想看到所有的,只要注册/
  • 白板是一种更简单、更好的方法
Http服务将找到最长的路径并调用该servlet。因此,这是一种退路

没有白板的hello world servlet示例:

@Component
public class Hello extends HttpServlet {
  public void doGet(HttpServletRequest rq, HttpServletResponse rsp) throws IOException {
    rsp.getWriter().write( ("Hello World " + rq.getParameter("name")).getBytes());
  }

  @Reference
  void setHttp(HttpService http) { http.registerService("/hello", null, null); }
}
@Component(provide=Servlet.class, properties="alias=/hello")
public class Hello extends HttpServlet {
  public void doGet(HttpServletRequest rq, HttpServletResponse rsp) throws IOException {
    rsp.getWriter().write( ("Hello World " + rq.getParameter("name")).getBytes());
  }
例如,现在使用白板:

@Component
public class Hello extends HttpServlet {
  public void doGet(HttpServletRequest rq, HttpServletResponse rsp) throws IOException {
    rsp.getWriter().write( ("Hello World " + rq.getParameter("name")).getBytes());
  }

  @Reference
  void setHttp(HttpService http) { http.registerService("/hello", null, null); }
}
@Component(provide=Servlet.class, properties="alias=/hello")
public class Hello extends HttpServlet {
  public void doGet(HttpServletRequest rq, HttpServletResponse rsp) throws IOException {
    rsp.getWriter().write( ("Hello World " + rq.getParameter("name")).getBytes());
  }
}


这种东西在bndtools中很容易使用。使用DS创建一个小项目,然后使用web控制台创建一个bndrun文件。我不会后悔的。

好吧,假设我想在doGet方法中构造一些HTML代码,带有带有一些GET参数的URL链接,比如/hellowordl?message=随便什么……注册HttpServlet时应该如何处理?因为如果我这样做,那么它似乎无法识别“?”后面的内容,因此没有调用RestServlet.doGet方法…不管怎样,我刚刚意识到我在doGet中生成的HTML代码上有一个输入错误…现在似乎可以工作了,谢谢:)你好,彼得,谢谢你的详细回答,但我有点迷路了…什么是bndtools和DS?我是这个Servlet的新手…如果我用别名/hello?param=foo构建URL…这行吗?在我的例子中,到目前为止它还没有,因此,我不确定如何注册Servlet以便它能够处理GET参数……听起来您在基本层面上感到困惑。别名=/hello使servlet在您浏览时被调用,返回“helloworld null”。如果您浏览到,servlet将收到'name'参数,并返回'Hello World Peter'。bndtools是一个基于Eclipse的OSGi IDE,请参阅DS是声明性服务,如果OSGi中没有这些服务,您将无法生存。请参阅DS和bndtools的介绍。是的,你是对的,我在一个非常基本的层面上弄错了:)谢谢你为我指明了正确的方向。我会检查bndtools,尽管我更习惯于Eclipse…谢谢!bndtools是一个eclipse插件,因此您可以在那里找到所有的好东西:-)bndtools取代了PDE,后者更倾向于eclipse插件开发,而不是OSGi捆绑包开发,尽管这些东西在理论上是相同的。只需转到Eclipse市场并搜索bndtools。