Get server IP address from JSP request / session object

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

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

+5
source share
5 answers

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

String serverIP = request.getLocalAddr();
+10
source
String sIPAddr = request.getRemoteAddr();
+3
source
String addr = request.getRemoteAddr();
+2

( , , ), :

            <%@ 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 %>
+2
request.getHeader("X_FORWARDED_FOR") 
0

All Articles