How to get the contextPath in a servlet's init-method?

Discussion in 'Java' started by Stephan Koser, Jan 14, 2005.

  1. Hi,

    is there a way to get the context path inside the servlet's init-method?
    In an URL like http://localhost/myApp/... i want to get the Attribute
    /myApp.

    I know, I can get ist with HttpServletRequest#getContextPath(), but in the
    init method I don't have a servletRequest.
    Is there another way to get it that also works with different Java
    Containers?

    ...
    bye Stephan...
    Stephan Koser, Jan 14, 2005
    #1
    1. Advertising

  2. Stephan Koser

    Guest

    Stephan Koser wrote:
    > Hi,
    >
    > is there a way to get the context path inside the servlet's

    init-method?
    > In an URL like http://localhost/myApp/... i want to get the Attribute
    > /myApp.
    >


    public void init() throws ServletException {
    ServletContext context = getServletContext();
    System.out.println("Real Path = " + context.getRealPath("/"));
    }
    , Jan 14, 2005
    #2
    1. Advertising

  3. wrote:

    > Stephan Koser wrote:
    >
    >>Hi,
    >>
    >>is there a way to get the context path inside the servlet's

    >
    > init-method?
    >
    >>In an URL like http://localhost/myApp/... i want to get the Attribute
    >>/myApp.
    >>

    >
    >
    > public void init() throws ServletException {
    > ServletContext context = getServletContext();
    > System.out.println("Real Path = " + context.getRealPath("/"));
    > }
    >


    That doesn't answer the question posed. The real path to the context
    root does not necessarily have anything at all to do with the context
    path relative to the server root.

    In answer to the OP's question, it is not possible to do what you want
    in general, because the same servlet may be accessible via more than one
    path. The context path is partly a function of the request, which is
    why it is only available from the request object. If you _really_ need
    this information at init time (and you probably don't), then you can
    fudge by putting it in one of the servlet's init parameters. This
    probably will introduce a dependency between the webapp's deployment
    descriptor and the servlet container's configuration, so do not
    undertake it lightly.


    John Bollinger
    John C. Bollinger, Jan 14, 2005
    #3
  4. Stephan Koser

    Guest

    Would setting a parameter/value pair to the startup servlet in the
    web.xml file and then using
    that be a satisfactory solution?
    , Jan 15, 2005
    #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. Replies:
    8
    Views:
    896
    John C. Bollinger
    Mar 4, 2005
  2. Replies:
    2
    Views:
    4,858
    William Brogden
    Feb 16, 2006
  3. Mitch
    Replies:
    2
    Views:
    553
    Mitch
    Mar 13, 2006
  4. crack_cs
    Replies:
    5
    Views:
    9,828
    Thomas Fritsch
    Mar 17, 2006
  5. Rich Morey
    Replies:
    9
    Views:
    4,792
    Arne Vajhøj
    Nov 1, 2008
Loading...

Share This Page