Map external directory to web.xml

点点圈 提交于 2019-11-30 10:33:11

No idea how to do it with Jetty, but in Tomcat you can just add a new <Context> to server.xml:

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

This way it's accessible by http://example.com/files/.... See if something similar exist for Jetty.

Update: after Googling, the "normal Java code" equivalent would be something like:

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

Now yet to translate that into jetty.xml flavor. I am a bit guessing based on the documentation and examples found on the web, so don't pin me on it:

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

Another possibility may be this:

<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>

After some more fiddling around, the best way to do this (for jetty) is to deploy a descriptor in the context directory that looks like the following...

<?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>

I hope that this helps someone else!

I don't know that you can do that as a URL mapping, however files and folders in the same directory that your web.xml file is in will be accessible in the way you describe, although you can't control the mapping under your webapp.

Does that suit your needs?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!