Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web容器中的OSGi-请求的资源不可用(JSP文件)_Jsp_Servlets_Osgi_Equinox_Osgi Bundle - Fatal编程技术网

Web容器中的OSGi-请求的资源不可用(JSP文件)

Web容器中的OSGi-请求的资源不可用(JSP文件),jsp,servlets,osgi,equinox,osgi-bundle,Jsp,Servlets,Osgi,Equinox,Osgi Bundle,我有个问题。我想用OSGi做一个网站。我对OSGi非常陌生,但我已经阅读了基础知识,并希望使用Equinox框架。为此,我阅读了《快速入门指南》,购买并阅读了《OSGi和Equinox——创建高度模块化的Java系统》一书 但回到我的问题上来。我从equinox站点下载了bridge.war,并用servlet创建了第一个包。激活器如下所示: public void start(BundleContext bundleContext) throws Exception { Activato

我有个问题。我想用OSGi做一个网站。我对OSGi非常陌生,但我已经阅读了基础知识,并希望使用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如下所示:

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);
}
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{ 执行任务(请求、响应); }

JSP文件位于捆绑包中的文件夹/WebContent/中。 如果我将捆绑包部署到bridge.war中,并尝试在浏览器中打开站点,我总是会得到以下结果:

我不知道如何配置Servlet或Activator,他们会找到jsp文件。 我试图将JSP文件移动到bridge.war中,但这无助于防止此问题

我想我必须在任何地方注册jsp文件(可能使用
httpService.registerResources(arg0,arg1,arg2);
),但我不知道如何正确地注册


我希望你能帮助我。

我终于解决了我的问题。我使用了扩展点。这是我的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>
停止:

通过这种配置,我可以使用

RequestDispatcher dispatcher = req.getRequestDispatcher("/webApp/start.jsp");
dispatcher.forward(req, resp);
在我的Servlet中没有问题


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

您已经在“/index”上下文中注册了servlet,您的URL中不应该有“index”吗?如果我打开servlet,它将被激活,并且在servlet中,我尝试将请求发送到test.jsp,但servlet找不到jsp文件。反过来如何,而是在OSGi容器中部署web应用程序。可以创建自己的捆绑包设置,也可以使用类似ApacheKaraf的OSGi容器。
@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);