users@glassfish.java.net

Re: How to get endpoint URL from ServletContext

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Thu, 11 Mar 2010 18:33:48 -0800

Hi Farrukh,

Farrukh Najmi wrote:
> Hi Jan,
>
> Thank you for your kind help.
>
> The hostname can be looked up. But How do I get the protocol scheme
> and port reliably?
> I am surprised that this seems so hard and that ServletContext does
> not have the endpoint readily available as a complete URL.

It's impossible for ServletContext to provide this info.

Assume you deploy foo.war to virtual server "bar" at context root "/foo".

Upon deployment, foo.war will be represented by a ServletContext whose
getContextPath() will return "/foo".

But the "foo" application may be accessed in different ways, depending
on your domain's configuration. For example, assume virtual server "bar"
receives requests from a non-secure HTTP listener at port 12345, and
from a secure HTTP listener at port 44444. Thus, the following requests
would be routed to the "foo" application:

  http://bar:12345/foo
  https://bar:44444/foo
>
> Is this not something that is needed very frequently?
> Is there a better way to do what I am trying to do?
>
Perhaps you could configure this info via context attributes?

> Thanks for any further help on this.
>

You're welcome.

Jan

> On 03/11/2010 07:04 PM, Jan Luehe wrote:
>>
>> Try using ServletContext#getContextPath, which returns an
>> application's prime or preferred context path (in case the
>> application has been deployed to more than one context root), and
>> prepend it with the desired scheme, domain (host) name, and port in
>> order to form a complete URL.
>>
>
> --
> Regards,
> Farrukh
>
> Web: http://www.wellfleetsoftware.com
>
>