将外部目录映射到 web.xml
-
18-09-2019 - |
题
有没有一种简单的方法可以映射 web.xml 或其他部署描述符(jetty.xml 等)文件中的目录?
例如,如果我有一个目录 /opt/files/ ,有没有一种方法可以通过访问来访问其文件和子目录 http://localhost/some-mapping/?我突然想到应该有一些简单的方法可以做到这一点,但我一直无法找到方法(通过谷歌,stackoverflow 等)。我发现的只是模仿文件服务器的 servlet,这不是我想要的。
作为参考,我在 AIX 机器上使用 jetty。
解决方案
不知道如何与码头做,但在Tomcat中你可以添加一个新的<Context>
到server.xml
:
<Context docBase="/opt/files" path="/files" />
这种方式是通过http://example.com/files/...
访问。看看是否有类似的存在码头。
<强>更新强>:谷歌搜索后, “正常的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>
其他提示
在一些摆弄周围,要做到这一点的最好办法(用于码头)是部署在如下所示的相关目录下一个描述符...
<?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>
我希望这可以帮助别人!
我不知道,你可以做,作为一个URL映射,但文件和文件夹在你的web.xml文件是会在你所描述的方式访问相同的目录,虽然你无法控制你的web应用下的映射。
这是否满足您的需求?
不隶属于 StackOverflow