users@glassfish.java.net

Re: Resolving the webroot directory

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Mon, 25 Jan 2010 16:39:51 -0800

On 01/25/10 02:48 PM, glassfish_at_javadesktop.org wrote:
> Hi,
>
> In my application I want users to be able to upload files to my server. Once uploaded, I want them to be accessible directly via HTTP, eg: http://mysite/a/directory/a_file
>
> My understanding is that to do this I need the files to be placed under the webroot directory. How can I discover the location of this directory at runtime?
>

Try using javax.servlet.ServletContext#getResource(String path):

    Returns a URL to the resource that is mapped to the given path.

    <p>The path must begin with a <tt>/</tt> and is interpreted
    as relative to the current context root,
    or relative to the <tt>/META-INF/resources</tt> directory
    of a JAR file inside the web application's <tt>/WEB-INF/lib</tt>
    directory.
    This method will first search the document root of the
    web application for the requested resource, before searching
    any of the JAR files inside <tt>/WEB-INF/lib</tt>.
    The order in which the JAR files inside <tt>/WEB-INF/lib</tt>
    are searched is undefined.

    <p>This method allows the servlet container to make a resource
    available to servlets from any source. [...]

If you must know the actual path, you can use
javax.servlet.ServletContext#getRealPath(String path):

    Gets the <i>real</i> path corresponding to the given
    <i>virtual</i> path.

    <p>For example, if <tt>path</tt> is equal to <tt>/index.html</tt>,
    this method will return the absolute file path on the server's
    filesystem to which a request of the form
    <tt>http://&lt;host&gt;:&lt;port&gt;/&lt;contextPath&gt;/index.html</tt>
    would be mapped, where <tt>&lt;contextPath&gt;</tt> corresponds to the
    context path of this ServletContext.

    <p>The real path returned will be in a form
    appropriate to the computer and operating system on
    which the servlet container is running, including the
    proper path separators.

Thanks,

Jan


> [Message sent by forum member 'agentjava' (tmurison_at_spectrumdt.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=382871
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>