7

我在 Jetty 服务器上有一个 Java Web 应用程序 (Eclipse/OSGI)。我希望能够从 Web 根目录之外的文件夹向我的 Web 应用程序提供静态文件。在我的 Web 应用程序中,我还不知道要提供的文件的文件名,因此我想在启动 Web 应用程序时将文件名(和/或路径)作为 VM 参数。例如:

我有一个图像 - myImg.jpg - 我已将它放在服务器文件系统上的一个文件夹中,例如 root/images/myImg.jpg。我想将此作为 VM 参数,例如“-DmyImg=/images/myImg.jpg/”,以便我可以获取图像并将其显示在我的网页上。我怎样才能做到这一点?我可以在不创建新 Servlet 的情况下做到这一点吗?

提前感谢您的帮助!

4

2 回答 2

11

解决了!

这是我添加到我的 jetty.xml 文件中的内容:

<Set name="handler">
    <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
            <Array type="org.eclipse.jetty.server.Handler">
                <Item>
                    <New class="org.eclipse.jetty.server.handler.ContextHandler">
                        <Set name="contextPath">/myContextPath</Set>
                        <Set name="handler">
                            <New class="org.eclipse.jetty.server.handler.ResourceHandler">
                                <Set name="directoriesListed">false</Set>
                                <Set name="resourceBase">/actual/folder/on/file/system</Set>
                            </New>
                        </Set>
                    </New>
                </Item>
                [...other handlers...]
            </Array>
        </Set>
    </New>
</Set>
于 2011-11-30T09:08:44.153 回答
3

@Farna:在您的回答中,我无法理解您如何将文件名作为 VM 参数传递。这就是我所做的。

testparvez.xml在码头webapps目录中创建了文件。

    <?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">/testparvez</Set>
  <Set name="resourceBase"><SystemProperty name="mydir"/></Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="welcomeFiles">
        <Array type="String">
          <Item><SystemProperty name="myfile"/></Item>
        </Array>
      </Set>
      <Set name="cacheControl">max-age=3600,public</Set>
    </New>
  </Set>
</Configure>

然后我开始码头作为

java -jar start.jar jetty.port=8082 -Dmydir=C:/test/javadoc/ -Dmyfile=index.html

最后我从 url 访问http://localhost:8082/testparvez/

于 2012-12-30T19:04:43.703 回答