开发者

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 ser开发者_如何学Gover name, not the IP address?


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

String serverIP = request.getLocalAddr();


String sIPAddr = request.getRemoteAddr();


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 %>


String addr = request.getRemoteAddr();


request.getHeader("X_FORWARDED_FOR") 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜