简体   繁体   English

如何启动Jetty8仅提供静态内容?

[英]How do I start Jetty8 to only serve static content?

For testing purposes I want to use Jetty 8 to serve only static content. 出于测试目的,我想使用Jetty 8仅提供静态内容。 I know how to start the webserver from the command line: 我知道如何从命令行启动Web服务器:

java -jar start.jar jetty.port=8082 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: 我希望能够使用香草码头,最好是8或7,并使用以下内容启动它:

java -jar start.jar OPTIONS=resources resources.root=../foo jetty.port=8082 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 . 应该可以通过http://localhost:8082/x.html访问名为../foo/x.html的文件。

I don't want to create a WAR file or anything fancy. 我不想创建一个WAR文件或任何花哨的东西。 Preferably it shouldn't do any caching on the server side, leaving the files unlocked on Windows machines. 最好不要在服务器端进行任何缓存,使文件在Windows机器上解锁。 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 我希望能够使用http://localhost:8080/javadoc/浏览Jetty 8附带的http://localhost:8080/javadoc/ ,但它总能给我一个404

java -jar start.jar --ini OPTIONS=Server,resources etc/jetty.xml contexts/javadoc.xml java -jar start.jar --ini OPTIONS =服务器,资源等/ jetty.xml contexts / javadoc.xml

The simplest way to start Jetty and have it serve static content is by using the following xml file: 启动Jetty并使其提供静态内容的最简单方法是使用以下xml文件:

static-content.xml: 静态的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: 你可以使用以下方式启动Jetty:

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; 如果省略files.base,将使用当前的目录; if you omit jetty.port, port 8080 will be used. 如果省略jetty.port,将使用端口8080。

The --ini will disable the settings from start.ini, therefore also make sure no other handlers etc. will be activated. --ini将禁用--ini的设置,因此也确保不会激活其他处理程序等。

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 ): 有点offtopic,但有人使用maven可能希望这样的事情(假设静态资源已被复制到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. 在contextxts目录下的发行版中有一个javadoc.xml,您可以将其作为一个示例来说明如何轻松地完成此操作。

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-distribution/src/main/resources/contexts/javadoc.xml 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 还建议只从start.ini文件中的启动中删除jetty-webapps.xml,并删除不想部署的上下文文件

you can look at setting some of the other options in the start.ini file as well if you like 您可以查看在start.ini文件中设置其他一些选项,如果您愿意的话

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

go there for information the start process 去那里获取开始过程的信息

cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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