Sophie

Sophie

distrib > Mandriva > 2010.0 > x86_64 > by-pkgid > c27466c2a3fa3cf6008c3a485d00ce04 > files > 112

jetty5-manual-5.1.15-1.5.2mdv2010.0.noarch.rpm

SECTION: 960-WebStart
TITLE: Eclipse
QUESTION: Can I embed Jetty in a Web Start application?

Using Java Web Start to load an application has several advantages. The application can be launched from a link on a web page. The user only has to have Java installed. Updates are automatic. The application jars are downloaded and run locally so impact on the external web server is minimal and performance is good after the initial download.
<p>
Unfortunately there are a few disadvantages as well. The application must be packaged entirely in jar files, and the name and path to the jar files is not known ahead of time.

If you embed a Jetty server in a JWS application, and want to serve static content, you must arrange for the server to be able to find the files. One solution is for the server to load the files from the classpath.
<p>
Here are the details:

<p>
A subclass of the Default servlet is used to serve the files. It is a very simple subclass, just overriding getResource() to get a resource. Here is the complete source:

<pre>
<code>
// ContentServlet.java
package com.skillsoft.meccano.server;

import java.io.IOException;
import java.net.URL;

import org.mortbay.jetty.servlet.Default;
import org.mortbay.util.Resource;

import com.skillsoft.meccano.util.ResourceLoader;

/**
 * This servlet serves static resources from the classpath. It allows static resources to be bundled in a jar.
 *  &lt;at&gt; author Kent Johnson
 */
public class ContentServlet extends Default {

    protected Resource getResource(String pathInContext) throws IOException {
        URL url = ResourceLoader.getResource(pathInContext);
        return Resource.newResource(url);
    }

}
</code>
</pre>
The actual resource loading is done in a separate module:
<p>
Here is ResourceLoader.getResource():
<p>
<pre>
<code>
    public static URL getResource(String name) {
        if (name == null || name.length()==0)
            return null;

        return ResourceLoader.class.getResource(name);
    }
</code>
</pre>
The ContentServlet is started just like any other servlet:
<pre>
<code>

    context.addServlet("ContentServlet", "/*", "com.skillsoft.meccano.server.ContentServlet");
</code>
</pre>
Note that this scheme exposes all of your classes to the webserver. If your server is widely available you might want to make the getResource() method more sophisticated in it's handling of resource requests.
<P>
<i>[Contributed by Kent Johnson 29-Feb-2004]</font>