You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Rick Mann <rm...@latencyzero.com> on 2001/08/28 05:28:17 UTC

Is there a way to get the "docBase" property from within a servlet?

Is there a Servlet spec-compliant way to get the webapp's directory's path
programmatically, from within a servlet? Something like calling
ServletConfig.getInitParameter("docBase"), but something that's standard,
and that does not require me to specify the path explicitly in a
configuration file?

TIA,

Rick


Re: Is there a way to get the "docBase" property from within a servlet?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sun, 2 Sep 2001, Jeff Turner wrote:

> Date: Sun, 2 Sep 2001 16:04:23 +1000
> From: Jeff Turner <je...@socialchange.net.au>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Re: Is there a way to get the "docBase" property from within a
>     servlet?
>
> On Sat, Sep 01, 2001 at 09:48:10PM -0700, Craig R. McClanahan wrote:
> >
> >
> > On Mon, 27 Aug 2001, Rick Mann wrote:
> >
> > > Date: Mon, 27 Aug 2001 20:28:17 -0700
> > > From: Rick Mann <rm...@latencyzero.com>
> > > Reply-To: tomcat-user@jakarta.apache.org
> > > To: tomcat-user@jakarta.apache.org
> > > Subject: Is there a way to get the "docBase" property from within a
> > >     servlet?
> > >
> > > Is there a Servlet spec-compliant way to get the webapp's directory's path
> > > programmatically, from within a servlet?
> >
> > No.
>
> getServletConfig().getServletContext().getRealPath("/");
>
> Of course it won't work within a .war file..
>

And therefore there is no *portable* solution, which was the original
question.

> > > Something like calling
> > > ServletConfig.getInitParameter("docBase"), but something that's standard,
> > > and that does not require me to specify the path explicitly in a
> > > configuration file?
> > >
> >
> > You are starting from an incorect assumption, that there *is* such a thing
> > as a portable "directory path" to a web application.  It is entirely legal
> > for a servlet container to run a web application directly from a WAR file
> > (in which case there is no expanded directory), or by storing its static
> > resources in some other sort of structure (such as being BLOB objects in a
> > database).
>
> Most websites are not static. Users publish content, for example, and
> the logical place for that content to be stored is on the filesystem
> inside the webapp, not in a database. So for many, or not most,
> websites, the notion that a website can be kept in a .war is not
> practical.
>

Although the statement might be true, the runtime implementation of web
application resources is totally up to the servlet container -- the only
requirement for WAR files is to accept them as the deployment mechanism.

Many (but by *no* means all) servlet containers do support an "open
directory" mechanism like Tomcat does.  If you write a web application
that depends on this (perhaps using the getRealPath("/") mechanism), then
you are limited to containers that give you this option.  That may be fine
for your needs -- but it's not portable, so there is therefore no portable
API (in javax.servlet) to access it under all circumstances.

> I'd be interested to know if there is a solution to this. Perhaps the
> filesystem could be mapped to a JNDI context, which could be made into a
> "virtual filesystem" inside the webapp?
>

As a matter of fact, this is exactly what Tomcat 4 does internally -- it
represents the static resources of a web app as a JNDI naming context.
But, because it is internal to Tomcat, it is not visible to applications.

There was some discussion in the JSR-053 expert group (the ones
responsible for Servlet 2.3) in making this kind of an abstraction visible
to applications, and therefore portable.  But, there wasn't enough time to
do it right for this round of the specs -- it will be considered again for
the next version.

> --Jeff
>
> > > TIA,
> > >
> > > Rick
> > >
> > >
> >
> > Craig McClanahan
> >
>

Craig



Re: Is there a way to get the "docBase" property from within a servlet?

Posted by Jeff Turner <je...@socialchange.net.au>.
On Sat, Sep 01, 2001 at 09:48:10PM -0700, Craig R. McClanahan wrote:
> 
> 
> On Mon, 27 Aug 2001, Rick Mann wrote:
> 
> > Date: Mon, 27 Aug 2001 20:28:17 -0700
> > From: Rick Mann <rm...@latencyzero.com>
> > Reply-To: tomcat-user@jakarta.apache.org
> > To: tomcat-user@jakarta.apache.org
> > Subject: Is there a way to get the "docBase" property from within a
> >     servlet?
> >
> > Is there a Servlet spec-compliant way to get the webapp's directory's path
> > programmatically, from within a servlet?
> 
> No.

getServletConfig().getServletContext().getRealPath("/");

Of course it won't work within a .war file..

> > Something like calling
> > ServletConfig.getInitParameter("docBase"), but something that's standard,
> > and that does not require me to specify the path explicitly in a
> > configuration file?
> >
> 
> You are starting from an incorect assumption, that there *is* such a thing
> as a portable "directory path" to a web application.  It is entirely legal
> for a servlet container to run a web application directly from a WAR file
> (in which case there is no expanded directory), or by storing its static
> resources in some other sort of structure (such as being BLOB objects in a
> database).

Most websites are not static. Users publish content, for example, and
the logical place for that content to be stored is on the filesystem
inside the webapp, not in a database. So for many, or not most,
websites, the notion that a website can be kept in a .war is not
practical.

I'd be interested to know if there is a solution to this. Perhaps the
filesystem could be mapped to a JNDI context, which could be made into a
"virtual filesystem" inside the webapp?

--Jeff

> > TIA,
> >
> > Rick
> >
> >
> 
> Craig McClanahan
> 

Re: Is there a way to get the "docBase" property from within a servlet?

Posted by Anand B N <an...@learningbyte.com>.
If you are not using WARs and using a folder for your web application then 
try :-

         ServletContext tContext = request.getServletContext();

         tContext.getRealPath("/");

Not sure if it works always but it works  and I guess most servlet engines 
also do function and retunr the docbase property this way.

Anand

At 03:00 PM 9/5/01 -0700, you wrote:


>On Wed, 5 Sep 2001, Rick Mann wrote:
>
> > Date: Wed, 05 Sep 2001 14:38:07 -0700
> > From: Rick Mann <rm...@latencyzero.com>
> > To: tomcat user jakarta.apache.org <to...@jakarta.apache.org>,
> >      Craig R. McClanahan <cr...@apache.org>
> > Subject: Re: Is there a way to get the "docBase" property from within a
> >     servlet?
> >
> > on 9/1/01 9:48 PM, Craig R. McClanahan at craigmcc@apache.org wrote:
> >
> > >> Is there a Servlet spec-compliant way to get the webapp's 
> directory's path
> > >> programmatically, from within a servlet?
> > >
> > > No.
> > >
> > >> Something like calling
> > >> ServletConfig.getInitParameter("docBase"), but something that's 
> standard,
> > >> and that does not require me to specify the path explicitly in a
> > >> configuration file?
> > >>
> > >
> > > You are starting from an incorect assumption, that there *is* such a 
> thing
> > > as a portable "directory path" to a web application.  It is entirely 
> legal
> > > for a servlet container to run a web application directly from a WAR file
> > > (in which case there is no expanded directory), or by storing its static
> > > resources in some other sort of structure (such as being BLOB objects 
> in a
> > > database).
> >
> > Okay. I get that. How does one access a resource, then, whether it's a file
> > in a directory or a file in a WAR file? For example, we have properties
> > files that specify certain things for our web app. Currently, I provide a
> > full path, from root, to this file as an init param to a servlet that gets
> > loaded at context startup.
> >
> > I'd like to be able to refer to this file relative to the web app's
> > directory (or within the web app's) .WAR file. Can you tell me how I'd go
> > about doing this, or what concepts to search for in the documentation to
> > answer this question?
> >
>
>The API calls for this are ServletContext.getResource() and
>ServletContext.getResourcesAsStream().  The path you specify is context
>relative, and must start with a slash ("/") character.  Details are in the
>servlet specification, available at:
>
>   http://java.sun.com/products/servlet/download.html
>
>For example, let's assume you wanted to parse the web app deployment
>descriptor for your application.  You can access it like this:
>
>   InputStream is =
>     getServletContext().getResourceAsStream("/WEB-INF/web.xml");
>
>The /WEB-INF directory is a good place to put resource files like this,
>because the servlet container will refuse to serve them in response to a
>direct request from a client.  Plus, this call will work no matter how
>your servlet container deploys your application (in a directory, in a WAR
>file, or whatever).  It also doesn't matter what context path your app
>ends up with, because the path is only relative to the context.
>
>In Servlet 2.3 (i.e. Tomcat 4.0), a new call was added that acts like the
>list() method of the java.io.File class.  For instance, to see the names
>of all the resources in the "/WEB-INF" directory, you can say:
>
>   Set paths =
>    getServletContext().getResourcePaths("/WEB-INF");
>
>and process all of them.  Tomcat 4 uses this, for example, to do directory
>listings when there is no welcome file in the selected directory.
>
>
> > As always, thanks for your help,
> >
> > ------------------------------------------------------------
> > Roderick Mann               rmann @ latencyzero.com.sansspam
> >
> >
> >
>
>Craig



Re: Is there a way to get the "docBase" property from within a servlet?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Wed, 5 Sep 2001, Rick Mann wrote:

> Date: Wed, 05 Sep 2001 14:38:07 -0700
> From: Rick Mann <rm...@latencyzero.com>
> To: tomcat user jakarta.apache.org <to...@jakarta.apache.org>,
>      Craig R. McClanahan <cr...@apache.org>
> Subject: Re: Is there a way to get the "docBase" property from within a
>     servlet?
>
> on 9/1/01 9:48 PM, Craig R. McClanahan at craigmcc@apache.org wrote:
>
> >> Is there a Servlet spec-compliant way to get the webapp's directory's path
> >> programmatically, from within a servlet?
> >
> > No.
> >
> >> Something like calling
> >> ServletConfig.getInitParameter("docBase"), but something that's standard,
> >> and that does not require me to specify the path explicitly in a
> >> configuration file?
> >>
> >
> > You are starting from an incorect assumption, that there *is* such a thing
> > as a portable "directory path" to a web application.  It is entirely legal
> > for a servlet container to run a web application directly from a WAR file
> > (in which case there is no expanded directory), or by storing its static
> > resources in some other sort of structure (such as being BLOB objects in a
> > database).
>
> Okay. I get that. How does one access a resource, then, whether it's a file
> in a directory or a file in a WAR file? For example, we have properties
> files that specify certain things for our web app. Currently, I provide a
> full path, from root, to this file as an init param to a servlet that gets
> loaded at context startup.
>
> I'd like to be able to refer to this file relative to the web app's
> directory (or within the web app's) .WAR file. Can you tell me how I'd go
> about doing this, or what concepts to search for in the documentation to
> answer this question?
>

The API calls for this are ServletContext.getResource() and
ServletContext.getResourcesAsStream().  The path you specify is context
relative, and must start with a slash ("/") character.  Details are in the
servlet specification, available at:

  http://java.sun.com/products/servlet/download.html

For example, let's assume you wanted to parse the web app deployment
descriptor for your application.  You can access it like this:

  InputStream is =
    getServletContext().getResourceAsStream("/WEB-INF/web.xml");

The /WEB-INF directory is a good place to put resource files like this,
because the servlet container will refuse to serve them in response to a
direct request from a client.  Plus, this call will work no matter how
your servlet container deploys your application (in a directory, in a WAR
file, or whatever).  It also doesn't matter what context path your app
ends up with, because the path is only relative to the context.

In Servlet 2.3 (i.e. Tomcat 4.0), a new call was added that acts like the
list() method of the java.io.File class.  For instance, to see the names
of all the resources in the "/WEB-INF" directory, you can say:

  Set paths =
   getServletContext().getResourcePaths("/WEB-INF");

and process all of them.  Tomcat 4 uses this, for example, to do directory
listings when there is no welcome file in the selected directory.


> As always, thanks for your help,
>
> ------------------------------------------------------------
> Roderick Mann               rmann @ latencyzero.com.sansspam
>
>
>

Craig



Re: Is there a way to get the "docBase" property from within a servlet?

Posted by Rick Mann <rm...@latencyzero.com>.
on 9/1/01 9:48 PM, Craig R. McClanahan at craigmcc@apache.org wrote:

>> Is there a Servlet spec-compliant way to get the webapp's directory's path
>> programmatically, from within a servlet?
> 
> No.
> 
>> Something like calling
>> ServletConfig.getInitParameter("docBase"), but something that's standard,
>> and that does not require me to specify the path explicitly in a
>> configuration file?
>> 
> 
> You are starting from an incorect assumption, that there *is* such a thing
> as a portable "directory path" to a web application.  It is entirely legal
> for a servlet container to run a web application directly from a WAR file
> (in which case there is no expanded directory), or by storing its static
> resources in some other sort of structure (such as being BLOB objects in a
> database).

Okay. I get that. How does one access a resource, then, whether it's a file
in a directory or a file in a WAR file? For example, we have properties
files that specify certain things for our web app. Currently, I provide a
full path, from root, to this file as an init param to a servlet that gets
loaded at context startup.

I'd like to be able to refer to this file relative to the web app's
directory (or within the web app's) .WAR file. Can you tell me how I'd go
about doing this, or what concepts to search for in the documentation to
answer this question?

As always, thanks for your help,

------------------------------------------------------------
Roderick Mann               rmann @ latencyzero.com.sansspam



Re: Is there a way to get the "docBase" property from within a servlet?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 27 Aug 2001, Rick Mann wrote:

> Date: Mon, 27 Aug 2001 20:28:17 -0700
> From: Rick Mann <rm...@latencyzero.com>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Is there a way to get the "docBase" property from within a
>     servlet?
>
> Is there a Servlet spec-compliant way to get the webapp's directory's path
> programmatically, from within a servlet?

No.

> Something like calling
> ServletConfig.getInitParameter("docBase"), but something that's standard,
> and that does not require me to specify the path explicitly in a
> configuration file?
>

You are starting from an incorect assumption, that there *is* such a thing
as a portable "directory path" to a web application.  It is entirely legal
for a servlet container to run a web application directly from a WAR file
(in which case there is no expanded directory), or by storing its static
resources in some other sort of structure (such as being BLOB objects in a
database).

> TIA,
>
> Rick
>
>

Craig McClanahan