users@glassfish.java.net

Re: Read file from docroot

From: Byron Nevins <Byron.Nevins_at_Sun.COM>
Date: Wed, 28 Nov 2007 00:21:35 -0800

Thanks Jan -- that works great.

To get the real path -- suitable for java.io.File operations, do this:

       ServletContext sc = getServletContext().getContext("/");
       File f = new File(sc.getRealPath("/index.html"));


Jan.Luehe_at_Sun.COM wrote:

> 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.
>>>
>>>
>>>
>>
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>

-- 
Byron Nevins Work 408-276-4089, Home 650-359-1290, Cell 650-784-4123 - Sun Microsystems, Inc.