Grohas
Grohas

Reputation: 23

Getting the user's IP in Java

How can I get the ip address of users who connect to my site located on my server using apache? I only get the ip address of my server.

protected void  getUserIp (){
    try {
        InetAddress ia = InetAddress.getLocalHost();
        String ipAddr = ia.getHostAddress();
        String hostName = ia.getHostName();
        log.debug("Local IP: " + ipAddr + "and" + "HOST: " + hostName);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Views: 109

Answers (2)

Omer Vertman
Omer Vertman

Reputation: 181

Is this code running inside a Spring controller ?

You can add a HttpServletRequest request argument to the controller method, which Spring will inject for you, and then you can do request.getRemoteAddr()

The HttpServletRequest object is coming from the Apache Tomcat

Upvotes: 1

Simon Martinelli
Simon Martinelli

Reputation: 36103

You could try:

UI.getCurrent().getSession().getBrowser().getAddress()

Upvotes: 1

Related Questions