我正在尝试注册一个将接收参数的HttpServlet(如果是通过POST或GET,请不要太在意,显然,明显优先选择POST)。几乎只是扩展了这里描述的内容:
http://www.javaworld.com/javaworld/jw-06-2008/jw-06-osgi3.html?page=3
在这里:
http://www.peterfriese.de/osgi-servlets-a-happy-marriage/
我没有使用仍然声明性注册,首先想看到它正常工作,然后我会做其他的事情。
在致电时出现疑问:
httpService.registerServlet("/helloworld", new RestServlet(), null, null);
不确定如何告诉HttpService服务器将接受params。此外,每次注册一个servlet时是否必须使用new()创建一个HttpServlet,或者我可以为不同的别名重用它吗?我问,因为也许可以在别名参数中使用一些通配符,然后让HttpServlet对象处理HttpRequest中的任何内容......?
欢迎任何帮助/建议/想法!
此致 亚历
答案 0 :(得分:2)
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());
}
}
这种东西在bndtools中很容易玩。使用DS创建一个小项目,然后使用Web控制台创建一个bndrun文件。不会后悔的。
答案 1 :(得分:0)
我不太了解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
等)。