users@glassfish.java.net

Re: Read file from docroot

From: <Jan.Luehe_at_Sun.COM>
Date: Tue, 27 Nov 2007 11:05:51 -0800

yonestar wrote:

>Byron,
>
>this was exactly what i needed!!
>thanks for the "ghastly hack" :-)
>
>

A cleaner way would be to acquire, from within your servlet, the
ServletContext
corresponding to the root context. Unless overridden by a
default-web-module,
this will give you the ServletContext corresponding to the
container-specific
webmodule deployed at "/" whose docbase corresponds to the virual server's
docroot, as follows:

        ServletContext sc = getServletContext().getContext("/");
        // Acquire URL for the docroot's index.html (or any other
resource stored in [...]/docroot)
        URL url = sc.getResource("/index.html");
        BufferedReader input = new BufferedReader(new
InputStreamReader(url.openStream()));
        // Start reading ...


Jan

>Y
>
>
>Byron Nevins wrote:
>
>
>>This is a ghastly hack but I use it to solve this exact problem.
>>
>>GlassFish's current directory is *guaranteed* to be <domain-dir>/config
>>
>>To get to the (default) docroot, I do this:
>>
>>File docroot = new File("../docroot")
>>
>>// optional but recommended
>>try { docroot = docroot.getCanonicalFile(); }
>>catch(IOException e) { docroot = docroot.getAbsoluteFile(); }
>>
>>It's not a great solution but it's better than hardcoding an absolute
>>path.
>>
>>
>>
>>
>
>
>