Map external directory to web.xml

2019-02-07 18:05发布

问题:

Is there an easy way to map a directory in the web.xml or other deployment descriptor (jetty.xml, etc) files?

For example, if I have a directory /opt/files/ is there a way that I can access its files and sub-directories by visiting http://localhost/some-mapping/? It strikes me that there should be some simple way of doing this, but I haven't been able to find out how (via google, stackoverflow, etc). All I've found are servlets that mimic fileservers, which is not what I would like.

For reference I am using jetty on an AIX box.

回答1:

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>


回答2:

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!



回答3:

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?