Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting servlet context path without a servlet request.

Reply
Thread Tools

Getting servlet context path without a servlet request.

 
 
stuart.j.wood@gmail.com
Guest
Posts: n/a
 
      05-22-2006
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

 
Reply With Quote
 
 
 
 
Stuart Wood
Guest
Posts: n/a
 
      05-22-2006
Much as I hate to reply to my own post, I've just found the answer -
for any who are interested, there's no facility for getting the context
path from the servlet context at the moment. It is being introduced in
the Servlet API 2.5 spec - see
http://www.jcp.org/aboutJava/communi...t-2.5_MR2.html
for a bit more detail.

Plan B, here we come.

Cheers
--
Stuart Wood

 
Reply With Quote
 
 
 
 
jmcgill
Guest
Posts: n/a
 
      05-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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?



To do what you want to do, you need a ContextListener, and you need to
declare it in web.xml. It's quite standard:
javax.servlet.ServletContextListener

This object is instantiated when your context loads your app, thus
getting you around the chicken-egg problem of needing to run your webapp
before being about to execute code based on its context.


James






 
Reply With Quote
 
Stuart Wood
Guest
Posts: n/a
 
      06-06-2006
jmcgill wrote:
> (E-Mail Removed) wrote:
>
> > 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?

>
>
> To do what you want to do, you need a ContextListener, and you need to
> declare it in web.xml. It's quite standard:
> javax.servlet.ServletContextListener
>
> This object is instantiated when your context loads your app, thus
> getting you around the chicken-egg problem of needing to run your webapp
> before being about to execute code based on its context.
>
>
> James


Thanks, James.
I'd dismissed this as unsolvable at the current version and moved on,
hence the late reply, but this doesn't help me either, I'm afraid. The
problem is not that I can't get the context, but more that I can't get
the context /path/ from the context.

The way I've solved it is to get the 'real path' to "/" and take the
last part of it as the context path. It's a bludgeon, and it may be
affected by other servlet containers, or upgrades to Tomcat, but it's
the best I can do until ServletContext.getContextPath() is implemented.

Cheers
--
Stuart Wood

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I get the servlet access url from servlet context shaoanqing@gmail.com Java 4 04-20-2007 09:10 AM
Servlet spec 2.4 doesn't know context path and initialization time??? Kimba Java 1 03-29-2007 12:53 AM
Passing Servlet Context to a non servlet object. Andrew Purser Java 3 11-04-2004 02:54 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat returning 404 error when using additional path (/servlet/myServlet/extra/path/info) Mupota Muchelemba Java 1 02-04-2004 12:43 AM



Advertisments