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 16:04:34 -0800

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