【发布时间】:2014-12-04 19:24:19
【问题描述】:
我有问题。我想将 OSGi 用于网站。我对 OSGi 很陌生,但我已经阅读了基础知识并想使用 Equinox 框架。为此,我阅读了来自http://eclipse.org/equinox/ 的快速入门指南,并购买并阅读了《OSGi 和 Equinox - 创建高度模块化的 Java 系统》一书
但回到我的问题。我从Equinox 站点下载了bridge.war,并创建了第一个带有servlet 的包。激活器如下所示:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
httpServiceTracker = new HttpServiceTracker(context);
httpServiceTracker.open();
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
httpServiceTracker.close();
httpServiceTracker = null;
}
private class HttpServiceTracker extends ServiceTracker {
public HttpServiceTracker(BundleContext context) {
super(context, HttpService.class.getName(), null);
}
public Object addingService(ServiceReference reference) {
HttpService httpService = (HttpService) context.getService(reference);
try {
httpService.registerServlet("/index", new StartServlet(), null, null); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
}
return httpService;
}
public void removedService(ServiceReference reference, Object service) {
HttpService httpService = (HttpService) service;
httpService.unregister("/index"); //$NON-NLS-1$
super.removedService(reference, service);
}
}
我的 Servlet 看起来像这样:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 执行任务(请求,响应); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
performTask(request, response);
}
private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
RequestDispatcher RequestDispatcherview = request.getRequestDispatcher("test.jsp");
RequestDispatcherview.forward(request, response);
}
JSP 文件位于文件夹 /WebContent/ 中,该文件夹位于 Bundle 中。 如果我将捆绑包部署到 bridge.war 并尝试在浏览器中打开该站点,我总是会得到以下信息:
我不知道如何配置我的 Servlet 或我的 Activator,以便他们找到 jsp 文件。 我试图将 JSP 文件移动到 bridge.war 中,但它并不能帮助我防止这个问题。
我想我必须在任何地方注册 jsp 文件(可能使用httpService.registerResources(arg0, arg1, arg2);),但我不知道如何正确地做到这一点。
我希望你能帮助我。
【问题讨论】:
-
你已经在“/index”上下文下注册了你的servlet,你的URL中不应该有“index”吗?
-
如果我打开localhost:8080/bridge/index,servlet 将被激活,在 servlet 中我尝试将请求分派给 test.jsp,但 servlet 找不到 jsp 文件。
-
不如反过来,将您的 Web 应用程序部署在 OSGi 容器中。要么创建您自己的捆绑包设置,要么使用像 Apache Karaf 这样的 OSGi 容器。
标签: jsp servlets osgi equinox osgi-bundle