5

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

informatik01
  • 15,636
  • 10
  • 72
  • 102
Hari kanna
  • 2,363
  • 6
  • 27
  • 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,040,783
  • 362
  • 3,548
  • 3,513
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,307
  • 2
  • 29
  • 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
  • 669,072
  • 92
  • 771
  • 1,162
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,236
  • 9
  • 35
  • 49