为什么我不能在Jetty 8.1.9中读取静态文件

时间:2013-03-05 04:53:23

标签: configuration jetty

在/ opt / jetty / webapps中,我在目录w下有test.xml。我在contexts目录中有这个test.xml:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/w</Set>
  <Set name="resourceBase">/opt/java/webapps/w/</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="welcomeFiles">
        <Array type="String">
          <Item>test.xml</Item>
        </Array>
      </Set>
      <Set name="cacheControl">max-age=3600,public</Set>
    </New>
  </Set>
</Configure>

为什么我不能阅读http://host/w/test.xml

1 个答案:

答案 0 :(得分:1)

您的问题有点令人困惑,因为您提到test.xml两次,并且在两个不同的目录中。

无论如何,这是使用download.eclipse.org/jetty/提供的标准jetty-distribution-8.1.9.v20130131.tar.gz设置所需内容的基本示例。

可部署的上下文

使用以下内容创建名为contexts/w.xml的文件

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/w</Set>
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/w/</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="welcomeFiles">
        <Array type="String">
          <Item>test.xml</Item>
          <Item>index.html</Item>
        </Array>
      </Set>
      <Set name="cacheControl">max-age=3600,public</Set>
    </New>
  </Set>
</Configure>

注意:

  • ${jetty.home}指向您/path/to/jetty-distribution-8.1.9.v20130131/
  • 的任何内容
  • 此上下文指向名为${jetty.home}/w/的目录,该目录在webapps目录中,这是故意的,因为webapps目录用于独立的Java Servlet或Java EE Web应用程序,以档案形式,或以展开的可部署形式。由于您使用的是ContextHandlerResourceHandler,因此您的部署不符合这些要求。

内容

${jetty.home}/w/目录中创建一些文件。

$ mkdir /path/to/jetty-distribution-8.1.9.v20130131/w
$ echo "<h1>Hello World</h1>" > /path/to/jetty-distribution-8.1.9.v20130131/w/index.html

测试

启动Jetty

$ cd /path/to/jetty-distribution-8.1.9.v20130131
$ java -jar start.jar

打开浏览器并进行测试

http://localhost:8080/w/