Skip to main content

How to get endpoint URL from ServletContext

6 replies [Last post]
Anonymous

Hi Guys,

I am using spring-ws in Glassfish V3 container and need to get endpoint
URL from ServletContext for my endpoint bean.

So I used the following code:

javax.servlet.ServletContext sc = ...
URL baseURL = sc.getResource("/");

The only problem is that above returns a URL like:

jndi:/server/myserver/

Where as I was expecting to get a URL like:

http://localhost:8080/myserver/

So it looks like I have to do a JNDI lookup. So I tried the following:

if (baseURL.getProtocol().equals("jndi")) {
//Resolve via JNDI lookup
Context jndiContext = new InitialContext();
String s = baseURL.toString();
Object o = jndiContext.lookup(s);
}

But above code does not work and gives the following exception:

javax.naming.NamingException: Lookup failed for 'server/myserver/' in
SerialContext [Root exception is javax.naming.NameNotFoundException:
server]

Looking at the server using the admin console I see no JNDI entries
under Resources / JNDI.

Can someone tell me what I need to do to get my servlet endpoint url
from the ServletContext in Glassfish V3? Thanks very much.

--
Regards,
Farrukh

Web: http://www.wellfleetsoftware.com

[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Farrukh Najmi

Dear GF colleagues,

Please let me know if m question is not clear. I would be very grateful
for some help on what seems to be a simple question worthy of an FAQ
entry. Thanks.

On 03/11/2010 10:31 AM, Farrukh Najmi wrote:
>
> Hi Guys,
>
> I am using spring-ws in Glassfish V3 container and need to get
> endpoint URL from ServletContext for my endpoint bean.
>
> So I used the following code:
>
> javax.servlet.ServletContext sc = ...
> URL baseURL = sc.getResource("/");
> The only problem is that above returns a URL like:
>
> jndi:/server/myserver/
> Where as I was expecting to get a URL like:
>
> http://localhost:8080/myserver/
> So it looks like I have to do a JNDI lookup. So I tried the following:
>
> if (baseURL.getProtocol().equals("jndi")) {
> //Resolve via JNDI lookup
> Context jndiContext = new InitialContext();
> String s = baseURL.toString();
> Object o = jndiContext.lookup(s);
> }
>
> But above code does not work and gives the following exception:
>
> javax.naming.NamingException: Lookup failed for 'server/myserver/' in
> SerialContext [Root exception is javax.naming.NameNotFoundException:
> server]
>
> Looking at the server using the admin console I see no JNDI entries
> under Resources / JNDI.
>
> Can someone tell me what I need to do to get my servlet endpoint url
> from the ServletContext in Glassfish V3? Thanks very much.
>
> --
> Regards,
> Farrukh
>
> Web:http://www.wellfleetsoftware.com
>
>

--
Regards,
Farrukh

Web: http://www.wellfleetsoftware.com

[att1.html]

Jan Luehe

Farrukh Najmi wrote:
>
> Dear GF colleagues,
>
> Please let me know if m question is not clear. I would be very
> grateful for some help on what seems to be a simple question worthy of
> an FAQ entry. Thanks.

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.

Jan

>
> On 03/11/2010 10:31 AM, Farrukh Najmi wrote:
>>
>> Hi Guys,
>>
>> I am using spring-ws in Glassfish V3 container and need to get
>> endpoint URL from ServletContext for my endpoint bean.
>>
>> So I used the following code:
>>
>> javax.servlet.ServletContext sc = ...
>> URL baseURL = sc.getResource("/");
>> The only problem is that above returns a URL like:
>>
>> jndi:/server/myserver/
>> Where as I was expecting to get a URL like:
>>
>> http://localhost:8080/myserver/
>> So it looks like I have to do a JNDI lookup. So I tried the following:
>>
>> if (baseURL.getProtocol().equals("jndi")) {
>> //Resolve via JNDI lookup
>> Context jndiContext = new InitialContext();
>> String s = baseURL.toString();
>> Object o = jndiContext.lookup(s);
>> }
>>
>> But above code does not work and gives the following exception:
>>
>> javax.naming.NamingException: Lookup failed for 'server/myserver/' in
>> SerialContext [Root exception is javax.naming.NameNotFoundException:
>> server]
>>
>> Looking at the server using the admin console I see no JNDI entries
>> under Resources / JNDI.
>>
>> Can someone tell me what I need to do to get my servlet endpoint url
>> from the ServletContext in Glassfish V3? Thanks very much.
>>
>> --
>> Regards,
>> Farrukh
>>
>> Web: http://www.wellfleetsoftware.com
>>
>>
>
>
> --
> Regards,
> Farrukh
>
> Web: http://www.wellfleetsoftware.com
>
>

[att1.html]

Farrukh Najmi

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.

Is this not something that is needed very frequently?
Is there a better way to do what I am trying to do?

Thanks for any further help on this.

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

[att1.html]

Jan Luehe

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

[att1.html]

Farrukh Najmi

Here is what I have learned so far...

If I want to get the precise protocol and port I have to get it from the
ServletRequest not the ServletContext. reason is, as Jan said this is
request specific an may vary from one request to another.

With spring-ws it is fairly straightforward to get this once a request
is being processed:

TransportContext tc =
TransportContextHolder.getTransportContext();
WebServiceConnection wc = tc.getConnection();
URI uri = wc.getUri();

Viola! the URI has exactly what I need :-)

Thanks very much Jan.

On 03/11/2010 09:33 PM, Jan Luehe wrote:
> 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
>>
>>
>

--
Regards,
Farrukh

Web: http://www.wellfleetsoftware.com

[att1.html]

Jan Luehe

Farrukh Najmi wrote:
>
> Here is what I have learned so far...
>
> If I want to get the precise protocol and port I have to get it from
> the ServletRequest not the ServletContext. reason is, as Jan said this
> is request specific an may vary from one request to another.
>
> With spring-ws it is fairly straightforward to get this once a request
> is being processed:
>
> TransportContext tc =
> TransportContextHolder.getTransportContext();
> WebServiceConnection wc = tc.getConnection();
> URI uri = wc.getUri();
>
> Viola! the URI has exactly what I need :-)
>
> Thanks very much Jan.
>

Excellent! :)

Jan

[att1.html]