On the JSP page, I want to get the IP address of the user viewing the page. How?

On the JSP page, I want to get the IP address of the user viewing the page. How can?

+4
source share
4 answers

Since scriptlets (those <% %> %% <% %> things) have been discouraged since a decade, here's an EL solution:

 <p>Your IP address is: ${pageContext.request.remoteAddr}</p> 

If you intend to use this for some business purposes, and not for showing purposes, then you should use the servlet. Then it is available HttpServletRequest#getRemoteAddr() .

+10
source
 <%= request.getRemoteAddr() %> 

Returns the IP address of the client or the last proxy server that sent the request. For HTTP servlets, as is the value of the CGI variable REMOTE_ADDR.

+6
source

Just add @Lauri's answer.

If the request came through a proxy server, the request should have a "Via" header .

However, there is no way to find out what the real IP address of the client is if there are intermediate proxies. And many people's browsers use proxies, regardless of whether they know about it.

+4
source

Answer Lurie

 <%= request.getRemoteAddr() %> 

does not provide the IP address of the client that requested the URl, but only provided the IP address assigned to the server.

If the request comes through a proxy server, you can use this:

 request.getHeader("X_FORWARDED_FOR") 

he will provide the IP address of the client.

+2
source

Source: https://habr.com/ru/post/1313355/


All Articles