How to determine which IP addresses my site was uploaded to?

I have a web application, and for this I want to grab the IP address of clients who access my website so that I can find out which region is most suitable for the application. I am using Java EE in my application.

Perhaps there is some solution if we talk about the header and when the request sent from the end user.

+8
java java-ee url jsp servlets
source share
3 answers

Use the getRemoteAddr () method from the ServletRequest interface or the getHeaders () methods of the HttpServletRequest form interface:

 HttpServletRequest httpRequest = (HttpServletRequest) request; String userIpAddress = httpRequest.getHeader("X-Forwarded-For"); 

There is one precaution when using the getRemoteAddr method:

Of course, you can use this method, and in general, you will get the IP address of the client. However, this method is useless if the user is behind a proxy server. In this case, you will get the IP address of the proxy server, not the client. But the proxy server may include the requesting IP address of the client in a special HTTP header. Thus, to get the method of calling the real client-IP getHeader("X-Forwarded-For") .

Example usage in JSP:

Use the given IP address value in the session using JSTL :

 <c:set var="userIp" value="${requestScope.header('x-forwarded-for')}" scope="session" /> 

And then get this value from the session in a convenient place.

In JSP you can use <c:out value="${sessionScope.userIp}" /> or in the servlet as session.getAttribute('userIp');

Read the docs :

java.lang.String getRemoteAddr () returns the IP address of the client or the last proxy that sent the request.

+15
source share

Along with the answers above (Andrew and Stephen), also use the analytics tool. This will give you a more detailed picture of the traffic coming to your site. One such example is Google Analytics .

+1
source share

The request object is passed as a Java argument to the servlet method generated from your JSP. Argument name ... wait ... request .

So, you should be able to reference it in the scriptlet built into your JSP; eg.

  <% InetAddress addr = request.getRemoteAddr(); // blah blah blah %> 

It's debatable whether this kind of thing is worth doing in the JSP. It is generally accepted as "best practice" that JSP should only be used to format output. The webapp business logic must reside in the controller servlet, which is then finally forwarded to the JSP servlet to generate HTML (or something else).

If you intend to incorporate business logic into JSPs, scriptlets are generally difficult to maintain ... although if you are just going to display an IP address, this should not be a concern. (The preferred approach from a technical point of view is to use tags and the JSP expression language.)

Finally, there is no guarantee that the IP address returned by getRemoteAddr() will be the actual IP address of the client. It can be a proxy address or even a complete manufacture.

0
source share

All Articles