8

I have a web application and for that I want to capture the IP address of the clients who are accessing my website so that I can know what region is the most accessing the application. I am using Java EE in my application.

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

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Prateek
  • 3,923
  • 6
  • 41
  • 79
  • Normally your hosting company would provide such features – adarshr Aug 24 '12 at 10:42
  • 2
    The servlet container of your choice will log all client accesses. If you want to get the IP programatically, use [ServletRequest.getRemoteAddr()](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRemoteAddr%28%29). – f_puras Aug 24 '12 at 10:43
  • Hey the API method you have told seems interesting. I actually used it on my servlet to which the request is being sent from the jsp page. But still it is not showing any value. Do I need to use request.getParameter method() for this purpose , But how ??? – Prateek Aug 24 '12 at 10:58

3 Answers3

16

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

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

There's one caution for using the method getRemoteAddr:

Sure you can use the method and in general case you will get IP of client. However, the method is useless if an user is behind a proxy. In this case you'll get the proxy server's IP address and not the client. But the proxy may include the requesting client IP in a special HTTP header. So to retrieve real-client-IP call method getHeader("X-Forwarded-For").

An example usage in JSP:

Use set value of IP address in 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 servlet as session.getAttribute('userIp');

Please read docs:

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

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
kapandron
  • 3,546
  • 2
  • 25
  • 38
  • Thanks for your right direction. But the fix here is where to use this method on the jsp which is having the form and submit button. Or inside the final servlet where it actually goes and then submits the data to database. – Prateek Aug 24 '12 at 11:02
  • You must have access to the `request`. Use set value of IP address in session using [JSTL](http://www.oracle.com/technetwork/java/index-jsp-135995.html): ``. And then get this value from the session in a convenient place. In JSP as `` or in servlet as `session.getAttribute('userIp');`. – kapandron Aug 24 '12 at 12:08
  • When I use this method then the output comes `null/` in the database ,whereas in case of getRemoteAddr() the database output comes as `0:0:0:0:0:1/:` what does this mean. – Prateek Aug 24 '12 at 12:09
  • It's a valid IPv6 address of your localhost. This is because that you are connecting via the local host. Try to use for request another computer has address different from `127.0.0.1`, and it'll be determined correctly. – kapandron Aug 24 '12 at 12:34
1

Along with the above said answers (by Andrey and Stephen) do use some analytics tool also. It will give you a bigger picture of the traffic coming to your website. One such example is Google Analytics.

pratikabu
  • 1,672
  • 14
  • 17
  • I have a google analytics account . But inside the application I want to plot a graph based on what Unique IP hits I have got. For that Google analytics can't help. – Prateek Aug 24 '12 at 11:29
  • @pKs: i agree. if u want to consume it within your application then u cannot use analytics. – pratikabu Aug 24 '12 at 11:48
0

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

So you should be able to refer to it in a scriptlet embedded in your JSP; e.g.

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

It is debatable whether you should be doing this kind of thing in a JSP. It is generally accepted as "best practice" that JSPs should only be used for formatting output. The business logic of a webapp should be in a controller servlet that then finally forwards to the JSP servlet to generate the HTML (or whatever).

If you are going to put business logic into a JSP, scriptlets tend to be difficult to maintain ... though if you are just going to display the IP address, that shouldn't be a concern. (The preferred approach from an engineering perspective is to use tags and JSP expression language.)

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    First, the method [getRemoteAddr()](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRemoteAddr%28%29) belongs to the interface [ServletRequest](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRemoteAddr%28%29) and it doesn't require cast a request to the [HttpServletRequest](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html). Besides, this method The method may not always return user IP address. In third the scriptlets are bad practice. – kapandron Aug 24 '12 at 11:30
  • I know the MVC but using a scrip let like above could help me somehow to resolve as its not possible even using a hidden parameter to get the IP address. To carry the parameter with request object I have to use the <%%> scrip let somewhere. But this is giving error. Do I need to place this in the same form which is sending data. – Prateek Aug 24 '12 at 11:32
  • In the event that you use only pure JSP then you can solve your issue by using scriptlets: `<% //your java code %>`. The fact that JSP eventually transformed into a servlet. Therefore this code will work the same way. This is an acceptable solution for a small test project but such code will inconvenient to understand and maintain if it will to develop and grow. – kapandron Aug 24 '12 at 12:33