【问题标题】:OSGi in Web-Container - The requested resource is not available (JSP File)Web 容器中的 OSGi - 请求的资源不可用(JSP 文件)
【发布时间】: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


【解决方案1】:

我终于解决了我的问题。我使用了扩展点。这是我的 plugin.xml:

<plugin>
    <extension point="org.eclipse.equinox.http.registry.httpcontexts">
        <httpcontext id="html">
            <resource-mapping path="/WebContent/static"/>
        </httpcontext>
    </extension>
    <extension point="org.eclipse.equinox.http.registry.resources">
        <resource alias="/webApp" httpcontextId="html"/>
    </extension>

    <extension point="org.eclipse.equinox.http.registry.httpcontexts">
        <httpcontext id="jsp">
            <resource-mapping path="/WebContent/dynamic"/>
        </httpcontext>
    </extension>
    <extension point="org.eclipse.equinox.http.registry.servlets">
        <servlet alias="/webApp/*.jsp" class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory" httpcontextId="jsp"/>
        <servlet alias="/webApp/Login" class="webfiles.servlets.Login"/>
    </extension>
</plugin>

我在 Activator 中的 Start 和 Stop 方法如下所示:

开始:

 @Override
 public void start(BundleContext bundleContext) throws Exception {
    super.start(bundleContext);
    Activator.context = bundleContext;

    Bundle jettBundle = Platform.getBundle("org.eclips.equinox.http.jetty");
    Bundle httpRegistryBundle = Platform.getBundle("org.eclipse.equinox.http.registry");
    try {
        jettBundle.start();
        httpRegistryBundle.start();
    } catch (Exception e) {
        // TODO: handle exception
    }
 }

停止:

@Override
public void stop(BundleContext bundleContext) throws Exception {
    super.stop(bundleContext);
    Activator.context = null;

    Bundle jettBundle = Platform.getBundle("org.eclips.equinox.http.jetty");
    Bundle httpRegistryBundle = Platform.getBundle("org.eclipse.equinox.http.registry");
    try {
        jettBundle.stop();
        httpRegistryBundle.stop();
    } catch (Exception e) {
        // TODO: handle exception
    }
 }

有了这个配置,我可以使用

RequestDispatcher dispatcher = req.getRequestDispatcher("/webApp/start.jsp");
dispatcher.forward(req, resp);

在我的 Servlet 中没有问题。

我希望我能帮助其他人解决同样的问题。

【讨论】:

    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 2013-01-06
    • 2021-12-04
    • 2015-12-09
    • 2016-09-21
    • 2013-03-08
    相关资源
    最近更新 更多