简体   繁体   中英

How do I start Jetty8 to only serve static content?

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

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.

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>

In your distribution under the contexts directory is a javadoc.xml that you can use as an example on how to do this easily enough.

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-distribution/src/main/resources/contexts/javadoc.xml

that is what it actually looks like

you are looking to change the context path and the resource base

would also recommend just removing jetty-webapps.xml from the startup in the start.ini file and also removing the context files you don't want to deploy with

you can look at setting some of the other options in the start.ini file as well if you like

http://wiki.eclipse.org/Jetty/Feature/Start.jar

go there for information the start process

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM