users@glassfish.java.net

Re: Read file from docroot

From: Byron Nevins <Byron.Nevins_at_Sun.COM>
Date: Thu, 08 Nov 2007 10:56:12 -0800

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.

yonestar wrote:

>Kedar, Jan, and others- thanks so much for the posts and discussion!! i've
>tried some of your proposed solutions, but i'm not finding something that
>works.
>
> it seems
>
>
>
>
>>String pathString = ServletContext.getRealPath("/index.html");
>>Could easily return:
>>"C:\Sun\AppServer\domains\domain1\applications\j2ee-modules\yourwar\index.html"
>>
>>
>>
>
>is exactly what i want. However, i cannot access the getRealPath method
>("non-static method cannot be accessed from static context"). i think this
>is b/c my program is not a servlet (does not extend any servlet
>interface/class).
>
>let me step back and describe again my goal. what i'm trying to do is:
>when someone clicks a link, i will generate a file from DB and then return
>that file to the user/browser. the way i was thinking of doing that was
>having the link's event handler first create the file, and then navigate to
>another page. that page would be the generated file or would re-direct to
>the generated file.
>
>my problem with the above is that the file i generate needs to be put in the
>docroot. but, i may not know in advance the absolute path of the docroot (so
>i don't know how to put the file there). a solution i'm considering for
>this is using a properties file to store the docroot path. is there a
>better way?
>
>thanks!!
>jon
>
>

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