Search code examples
jspservletsosgiequinoxosgi-bundle

OSGi in Web-Container - The requested resource is not available (JSP File)


i have a problem. I want to use OSGi for a Website. I am pretty new to OSGi but i have read the basics and want to use the framework Equinox. For this i had read the Quick Start Guide from http://eclipse.org/equinox/ and bought and read the Book "OSGi and Equinox - Creating Highly Modular Java Systems"

But back to my problem. I downloaded the bridge.war from the equinox site and createt a first bundle with a servlet. The Activator look like this:

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);
}
}

And my Servlet look like this:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { performTask(request, response); }

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);
}

The JSP File is in the Folder /WebContent/ which is in the Bundle. If i deploy the bundle into the bridge.war and try to open the site in a Browser, i always get the following:

enter image description here

I do not know how i can configure my Servlet or my Activator, that they will find the jsp file. I tried to move the JSP File into the bridge.war but it does not help me to prevent this problem.

I guess i have to register the jsp file anywhere (maybe with httpService.registerResources(arg0, arg1, arg2);) but i do not know how i do this properly.

I hope you can help me.


Solution

  • I finally solved my Problem. I used extension points. Here is my 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>
    

    My Start and Stop methods in the Activator are looking like this:

    Start:

     @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
        }
     }
    

    Stop:

    @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
        }
     }
    

    With this configuration i can use

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

    in my Servlet without problems.

    I hope I can help others with the same Problem.