Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start Jetty8 to only serve static content?

Tags:

jetty

For testing purposes I want to use Jetty 8 to serve only static content. I know how to start the webserver from the command line:

java -jar start.jar jetty.port=8082

I would like to be able to use a vanilla Jetty, preferably 8 or 7, and start it using something like:

java -jar start.jar OPTIONS=resources resources.root=../foo jetty.port=8082

The files should then be accessible from the root of the server. A file called ../foo/x.html should be accessible via http://localhost:8082/x.html.

I don't want to create a WAR file or anything fancy. Preferably it shouldn't do any caching on the server side, leaving the files unlocked on Windows machines. Also, I only want to serve files, even located in subdirectories, no fancy file browser or ways to modify them from a client.

Is this possible? If not, what is the minimum configuration needed to accomplish such behavior?

Additional information

I've tried the following command. I expected to be able to browse the javadoc shipped with Jetty 8 using http://localhost:8080/javadoc/, but it always gives me a 404

java -jar start.jar --ini OPTIONS=Server,resources etc/jetty.xml contexts/javadoc.xml

like image 856
Roel Spilker Avatar asked Jun 28 '12 13:06

Roel Spilker


People also ask

How do I make a static server content?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.

How Jetty server works?

The Jetty Server is the plumbing between a collection of Connectors that accept HTTP connections, and a collection of Handlers that service requests from the connections and produce responses, with the work being done by threads taken from a thread pool.


2 Answers

The simplest way to start Jetty and have it serve static content is by using the following xml file:

static-content.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="FileServer" class="org.eclipse.jetty.server.Server">
    <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="host"><Property name="jetty.host" /></Set>
            <Set name="port"><Property name="jetty.port" default="8080"/></Set>
          </New>
      </Arg>
    </Call>

    <Set name="handler">
      <New class="org.eclipse.jetty.server.handler.ResourceHandler">
        <Set name="resourceBase"><Property name="files.base" default="./"/></Set>
      </New>
    </Set>
</Configure>

Than you can start Jetty using:

java -jar start.jar --ini static-content.xml files.base=./foo jetty.port=8082

If you omit files.base, the current direcory will be used; if you omit jetty.port, port 8080 will be used.

The --ini will disable the settings from start.ini, therefore also make sure no other handlers etc. will be activated.

like image 61
Roel Spilker Avatar answered Nov 01 '22 13:11

Roel Spilker


A bit of offtopic, but somebody using maven may wish to this something like this (supposing that static resources have been copied to target/web):

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.9.v20130131</version>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>install</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <webAppConfig>
                    <resourceBases>
                        <contextPath>/</contextPath>
                        <resourceBase>${project.build.directory}/web</resourceBase>
                    </resourceBases>
                </webAppConfig>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 20
dma_k Avatar answered Nov 01 '22 11:11

dma_k