2010-03-08 127 views
2

有没有简单的方法来映射web.xml或其他部署描述符(jetty.xml等)文件中的目录?将外部目录映射到web.xml

例如,如果我有一个目录/ opt/files /有没有办法通过访问http://localhost/some-mapping/来访问其文件和子目录?它让我感到应该有一些简单的方法来做到这一点,但我一直无法找到(通过谷歌,stackoverflow等)。我发现的所有servlet都是模仿文件服务器的,这不是我想要的。

仅供参考我在AIX机器上使用jetty。

回答

5

不知道如何与码头做,但在Tomcat中你可以添加一个新的<Context>server.xml

<Context docBase="/opt/files" path="/files" /> 

这种方式是通过http://example.com/files/...访问。看看Jetty是否有类似的东西存在。

更新:谷歌搜索后, “正常的Java代码” 等价会是这样的:

WebAppContext files = new WebAppContext("/opt/files", "/files"); 
Server server = new Server(8080); 
server.setHandler(files); 
server.start(); 

现在还没有翻译成jetty.xml味道。我有点基于猜测在网络上找到的文档和例子,所以不要束缚我就可以了:

<Configure class="org.mortbay.jetty.webapp.WebAppContext"> 
    <Set name="webApp">/opt/files</Set> 
    <Set name="contextPath">/files</Set> 
</Configure> 

另一种可能性可能是这样的:

<Configure class="org.mortbay.jetty.Server"> 
    <Call name="addHandler"> 
     <Arg> 
      <New class="org.mortbay.jetty.webapp.WebAppContext"> 
       <Arg name="webApp">/opt/files</Arg> 
       <Arg name="contextPath">/files</Arg> 
      </New> 
     </Arg> 
    </Call> 
</Configure> 
+0

谢谢,这绝对是在Tomcat(我更熟悉)上做到这一点的方法。我认为相应的jetty文件被称为jetty.xml。用于在jetty上映射目录的xml语言看起来与tomcat完全不同。 – 2010-03-08 21:16:51

+0

感谢您的更新。不幸的是,我正在使用eclipse jetty API,这有点不同。第一个建议根本不起作用,第二个建议不起作用,因为eclipse jetty中没有“addHandler”方法http://download.eclipse.org/jetty/stable-7/apidocs/。 – 2010-03-08 21:53:07

+1

我明白了。这个FileServer示例有帮助吗? http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#File_Server – BalusC 2010-03-08 22:05:03

0

我不知道您可以将其作为URL映射来执行此操作,但是您的web.xml文件所在的同一目录中的文件和文件夹将以您描述的方式访问,尽管您无法控制在你的webapp下的映射。

这是否适合您的需求?

+0

谢谢,但我知道这。我正在寻找一种方法来映射Web容器外部的目录。我知道这可能不是最佳做法,但这是一个服务器,它将与互联网保持隔离,并且只能从内部网访问。 – 2010-03-08 20:52:55

+1

该根目录中的快捷方式(UNIX软链接或Windows快捷方式)? – Brabster 2010-03-08 21:00:55

+0

UNIX软链接可能工作,但Windows快捷方式不(我刚测试过)。我想我已经找到了自己的问题的答案(在一些沉重的谷歌搜索之后)。请参阅下面的答案。 – 2010-03-08 21:07:34

1

经过一些摆弄周围,最好的方式做到这一点(对码头)是部署在如下所示的相关目录下一个描述符...

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

<!-- Configuration of a custom context. --> 
<Configure class="org.eclipse.jetty.server.handler.ContextHandler"> 
    <Call class="org.eclipse.jetty.util.log.Log" name="debug"> 
     <!-- The default message to show if our context breaks. --> 
     <Arg>Configure context.xml</Arg> 
    </Call> 
    <!-- 
     The context path is the web location of the context in relation to the 
     server address. 
    --> 
    <Set name="contextPath">/context</Set> 
    <!-- 
     The resource base is the server directory to use for fetching files. 
    --> 
    <Set name="resourceBase">/path/to/files/on/server</Set> 
    <Set name="handler"> 
     <New class="org.eclipse.jetty.server.handler.ResourceHandler"> 
      <Set name="directoriesListed">true</Set> 
      <!-- For now we don't need any welcome files --> 
      <!-- 
       <Set name="welcomeFiles"> <Array type="String"> 
       <Item>index.html</Item> </Array> </Set> 
      --> 
      <!-- 
       The cache time limit in seconds (ie max-age=3600 means that if the 
       document is older than 1 hour a fresh copy will be fetched). 
      --> 
      <Set name="cacheControl">max-age=3600,public</Set> 
     </New> 
    </Set> 
</Configure> 

我希望这可以帮助别人其他!

相关问题