Getting servlet context path without a servlet request.

Discussion in 'Java' started by stuart.j.wood@gmail.com, May 22, 2006.

  1. Guest

    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
    , May 22, 2006
    #1
    1. Advertising

  2. Stuart Wood Guest

    Stuart Wood, May 22, 2006
    #2
    1. Advertising

  3. jmcgill Guest

    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
    jmcgill, May 23, 2006
    #3
  4. Stuart Wood Guest

    jmcgill wrote:
    > 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
    Stuart Wood, Jun 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    1,993
    Jack Jia
    Apr 4, 2004
  2. Andrew Purser
    Replies:
    3
    Views:
    5,074
    Alex Kay
    Nov 4, 2004
  3. Kimba
    Replies:
    1
    Views:
    1,284
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Mar 29, 2007
  4. Replies:
    4
    Views:
    685
    santax
    Apr 20, 2007
  5. Norah Jones
    Replies:
    0
    Views:
    71
    Norah Jones
    Apr 7, 2014
Loading...

Share This Page