users@jersey.java.net

Re: [Jersey] Requesting client IP

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 17 Jun 2008 13:59:03 -0400

On Jun 17, 2008, at 1:06 PM, Magic Badger wrote:

> Long time user, first time poster.
>
> Is there any way for a resource to be able to retrieve the IP address
> of the requesting client? I would imagine that the HttpServletRequest
> object would contain such informatio, but suspect there is no way to
> access this using Jersey.
>
> Any help much appreciated!
>
HttpServletRequest.getRemoteAddr() will do what you want. To access
the HttpServletRequest in a resource class just add a field:

@Context HttpServletRequest hsr;

and the runtime will inject it on the resource class instance. You can
also use a constructor or method parameter if that is more convenient.

Obviously this will only work if the app is hosted in a Servlet
container.

Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.