You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Stuart Wood <st...@gmail.com> on 2006/05/22 12:22:38 UTC

Getting servlet context path without a servlet request.

I have a web application running under Tomcat 5.0, and I need to write
some output to a file.
This app doesn't have its servlet context hardcoded in anywhere, so I
can deploy it with any name and not worry about missing some random bit
of code anywhere, and I often deploy it with different names so the
customers can compare 2 versions side by side.

My problem is that I need to output to a log file within the
application, and I'd like the logfile name to be the context name, so
that I don't have to worry about 2 separate deployments inadvertently
writing to the same file.
I can get the context path from the first request that comes in, but
until that point, I don't seem to be able to retrieve it from either
the ServletContext or the ServletConfig...
Am I missing something?

I'm running in Tomcat, so I can hack it using some Tomcat-specific
code, but I'd rather leave it as portable as possible. Anyone have any
ideas?
Cheers in advance
-- 
Stuart Wood

Re: Getting servlet context path without a servlet request.

Posted by Stuart Wood <st...@gmail.com>.
Thanks, Tim.

I'd just found a page on the 2.5 spec saying it was on its way.

I think I'm going to just use ServletContext.getServletContextName() and
just remember to keep them in step myself - I guess it's only an issue
during development anyway, but it seemed like such an elegant solution.

Cheers
--
Stuart Wood


On 5/22/06, Tim Funk <fu...@joedog.org> wrote:
>
> Your on the right track - the servlet 2.5 spec fixes this (but tomcat 6
> isn't
> out yet).
>
> If I were in this predicament - I'd use a ServletContext init parameter.
> They
> can be overridden in the <Context> declaration.
>
> -Tim
>
> Stuart Wood wrote:
> > I have a web application running under Tomcat 5.0, and I need to write
> > some output to a file.
> > This app doesn't have its servlet context hardcoded in anywhere, so I
> > can deploy it with any name and not worry about missing some random bit
> > of code anywhere, and I often deploy it with different names so the
> > customers can compare 2 versions side by side.
> >
> > My problem is that I need to output to a log file within the
> > application, and I'd like the logfile name to be the context name, so
> > that I don't have to worry about 2 separate deployments inadvertently
> > writing to the same file.
> > I can get the context path from the first request that comes in, but
> > until that point, I don't seem to be able to retrieve it from either
> > the ServletContext or the ServletConfig...
> > Am I missing something?
> >
> > I'm running in Tomcat, so I can hack it using some Tomcat-specific
> > code, but I'd rather leave it as portable as possible. Anyone have any
> > ideas?
> > Cheers in advance
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
--
Stuart Wood

Re: Getting servlet context path without a servlet request.

Posted by Tim Funk <fu...@joedog.org>.
Your on the right track - the servlet 2.5 spec fixes this (but tomcat 6 isn't 
out yet).

If I were in this predicament - I'd use a ServletContext init parameter. They 
can be overridden in the <Context> declaration.

-Tim

Stuart Wood wrote:
> I have a web application running under Tomcat 5.0, and I need to write
> some output to a file.
> This app doesn't have its servlet context hardcoded in anywhere, so I
> can deploy it with any name and not worry about missing some random bit
> of code anywhere, and I often deploy it with different names so the
> customers can compare 2 versions side by side.
> 
> My problem is that I need to output to a log file within the
> application, and I'd like the logfile name to be the context name, so
> that I don't have to worry about 2 separate deployments inadvertently
> writing to the same file.
> I can get the context path from the first request that comes in, but
> until that point, I don't seem to be able to retrieve it from either
> the ServletContext or the ServletConfig...
> Am I missing something?
> 
> I'm running in Tomcat, so I can hack it using some Tomcat-specific
> code, but I'd rather leave it as portable as possible. Anyone have any
> ideas?
> Cheers in advance

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org