5

In JSP page I want to get IP address of user who view the page. How can ?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Hari kanna
  • 2,461
  • 6
  • 29
  • 43

4 Answers4

11

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().

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
6
<%= 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.

Lauri Lehtinen
  • 10,647
  • 2
  • 31
  • 29
4

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

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.

Satish Sharma
  • 3,284
  • 9
  • 38
  • 51