Skip to main content

Get client IP address with Java 6 server

3 replies [Last post]
hochi
Offline
Joined: 2008-06-10

Hi

I created a web service server implementation from WSDL with JAX-WS 2.1.1 that comes with java 6u6. Now I need the requesting client IP address for processing.

I tried to get the IP address from the message context but context.get(MessageContext.SERVLET_REQUEST) always returns null, so I can't get the IP address out of the HttpServletRequest object :-(

I tried using the newest implementation (JAX-WS 2.1.3) but the HttpServletRequest object is still null.

Is there a another way to get the client IP address ?

Help is very appreciated

regards

Message was edited by: hochi

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jitu
Offline
Joined: 2003-06-14

SERVLET_REQUEST is available only when it is deployed in a servlet container. Endpoint.publish() api uses a light weight http server. If you are using jax-ws runtime in JDK, we don't provide any extensions.

But if you use 2.1.3 in the classpath, you should see HttpExchange object of light weight http serve r in the MessageContext. The property is JAXWSProperties.HTTP_EXCHANGE. You could use this object to get client's IP address.

hochi
Offline
Joined: 2008-06-10

Hi jitu

Thank you very much for the helpful information.

I successfully got the client IP address from HttpExchange.

Nostromo
Offline
Joined: 2011-04-07

Hi
I need to obtain IP address in server side I am using metro 1.5 and java 1.6 but I dont know how to access to context from server side

@javax.jws.WebService (endpointInterface="ws.server.WsConsultaOnlinePortType",serviceName="WsConsultaOnlineService" )
public class wsConsultaDirectaImpl extends WSCoreBase {

// @Resource
WebServiceContext wsctx;

/* Comprueba que servicio este levantado
* @return true
*/
public boolean test() {
System.out.println("vacio1");
MessageContext msgContext = wsctx.getMessageContext();
System.out.println("vacio2");
Map requestHeaders = (Map) msgContext.get(MessageContext.HTTP_REQUEST_HEADERS) ;
System.out.println("vacio3");
if (requestHeaders!=null)
if (requestHeaders.isEmpty()) System.out.println("vacio");
else
System.out.println("no vacio");

return true;
}
MessageContext msgContext = wsctx.getMessageContext(); throw a null pointer exception
May i help me ¿