In JSP page I want to get IP address of user who view the page. How can ?
-
What is the underlying problem you need to solve? – Thorbjørn Ravn Andersen Jun 21 '10 at 08:53
4 Answers
Since scriptlets (those <% %>
things) are discouraged since a decade, here's the EL solution:
<p>Your IP address is: ${pageContext.request.remoteAddr}</p>
If you actually intend to use this for some business purposes rather than displaying purposes, then you should be using a servlet. It's then available by HttpServletRequest#getRemoteAddr()
.

- 1,082,665
- 372
- 3,610
- 3,555
<%= request.getRemoteAddr() %>
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. For HTTP servlets, same as the value of the CGI variable REMOTE_ADDR.

- 10,647
- 2
- 31
- 29
Just to add to @Lauri's answer.
If the request came via a proxy, there should be a "Via" header in the request.
However, there is no way to figure out what the real client IP address if there are any intermediate proxies. And a lot of peoples' browsers use proxies, whether or not they are aware of it.

- 698,415
- 94
- 811
- 1,216
Luri's answer
<%= request.getRemoteAddr() %>
doesn't provide the IP Address Of Client who requested the URl, but would only provide the IP assigned to Server.
If the request comes via a proxy then you can use this :
request.getHeader("X_FORWARDED_FOR")
it will provide the IP of client.

- 3,284
- 9
- 38
- 51