4 Replies Latest reply on Nov 21, 2003 4:20 PM by jonlee

    Programmatic Access to Server URL

    hreynolds2

      Is there a mgmt API that will allow me to get the URL that the server is running on from within a Servlet? Presumably all I need is the port that it's running on.

      thanks for any tips,

      ~harris

        • 1. Re: Programmatic Access to Server URL
          jonlee

          Have a look at the various request APIs for servlets (HttpServletRequest and parent).

          request.getServerPort()
          request.getServerName()
          request.getURI()

          And so on.

          • 2. Re: Programmatic Access to Server URL
            hreynolds2

            gotta love the newbie questions!!! thx,

            ~harris

            • 3. Re: Programmatic Access to Server URL
              hreynolds2

              There is a key piece to my question that I left out.... is there a way to get the port of the server you are running on in the servlet init method (*before* the first request comes in)?

              That's the API I'm looking for and I'm imagining it'd need to be container specific.

              thanks,

              ~harris

              • 4. Re: Programmatic Access to Server URL
                jonlee

                AFAIK, no. The servlet has no concept of this since it can serve any request passed on to it from a listener/connector. So you could have a listener on port 8080 passing a request on, as well as a listener on port 80 doing the same. The init is used when the servlet is first created, but once created, it can receive requests from any listener/connector on the server. That's why the information on the URI, etc is contained in the request.

                You could specifically provide a value via initparam by I would think this is a bit inflexible. YMMV.