Lydon Ch
Lydon Ch

Reputation: 8815

Get Server IP address from JSP Request/session object

How can I get the IP address of the server from a JSP page?

Right now, all I can do is request.getLocalName(), which returns the server name, not the IP address?

Upvotes: 6

Views: 33862

Answers (5)

Aaron Saunders
Aaron Saunders

Reputation: 33345

String addr = request.getRemoteAddr();

Upvotes: 2

Satish Sharma
Satish Sharma

Reputation: 3294

request.getHeader("X_FORWARDED_FOR") 

Upvotes: 0

Nux
Nux

Reputation: 10002

To get an actual server IP and hostname (actual and not set by e.g. a proxy) use this:

            <%@ page import="java.net.*" %> 
            [...]
            <%
            String hostname, serverAddress;
            hostname = "error";
            serverAddress = "error";
            try {
                InetAddress inetAddress;
                inetAddress = InetAddress.getLocalHost();
                hostname = inetAddress.getHostName();
                serverAddress = inetAddress.toString();
            } catch (UnknownHostException e) {

                e.printStackTrace();
            }
            %>
            <li>InetAddress: <%=serverAddress %>
            <li>InetAddress.hostname: <%=hostname %>

Upvotes: 3

ig0774
ig0774

Reputation: 41257

Actually, for the IP address of the server, you need to use

String serverIP = request.getLocalAddr();

Upvotes: 13

Abhijeet Pathak
Abhijeet Pathak

Reputation: 1948

String sIPAddr = request.getRemoteAddr();

Upvotes: 3

Related Questions