2014-12-04 135 views
1

我遇到问题。我想使用OSGi作为网站。我对OSGi相当陌生,但我已经阅读了基础知识并希望使用框架Equinox。为此,我从http://eclipse.org/equinox/阅读了快速入门指南,并购买了书籍“OSGi和Equinox - 创建高度模块化的Java系统”Web-Container中的OSGi - 请求的资源不可用(JSP文件)

但是回到我的问题。我从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是这样的:

保护无效的doGet(HttpServletRequest的请求,HttpServletResponse的响应)抛出了ServletException,IOException异常{ performTask(请求,响应); }

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 /这在捆绑。 如果我部署束引入Bridge.war,然后尝试在浏览器中打开网站,我总是得到如下:

enter image description here

我不知道我可以配置我的Servlet或者我的活化剂,他们会找到jsp文件。 我试图将JSP文件移动到bridge.war中,但它不能帮助我防止此问题。

我想我必须在任何地方注册jsp文件(可能与httpService.registerResources(arg0, arg1, arg2);),但我不知道我是如何正确执行此操作的。

我希望你能帮助我。

+0

你已经在“/ index”上下文中注册了你的servlet,你不应该在你的URL的某个地方有“index”吗? – 2014-12-05 11:57:44

+0

如果我打开http:// localhost:8080/bridge/index,servlet将被激活,并且在servlet中,我尝试将请求发送到test.jsp,但servlet无法找到jsp文件。 – Baalthasarr 2014-12-05 14:20:06

+0

另一方面,将您的Web应用程序部署到OSGi容器中。要么创建你自己的捆绑设置,要么使用Apache Karaf这样的OSGi容器。 – 2014-12-05 21:45:23

回答

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> 
在激活

我的启动和停止方法正在寻找这样的:

开始:

@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中没有问题。

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