2010-03-08 82 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> 

我希望這可以幫助別人其他!