从 JSP 请求/会话对象获取服务器 IP 地址

发布于 2024-09-08 19:45:58 字数 87 浏览 3 评论 0原文

如何从JSP页面获取服务器的IP地址?

现在,我能做的就是 request.getLocalName(),它返回服务器名称,而不是 IP 地址?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

李不 2024-09-15 19:45:58

实际上,对于服务器的IP地址,您需要使用

String serverIP = request.getLocalAddr();

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

String serverIP = request.getLocalAddr();
涫野音 2024-09-15 19:45:58
String sIPAddr = request.getRemoteAddr();
String sIPAddr = request.getRemoteAddr();
宛菡 2024-09-15 19:45:58

要获取实际的服务器 IP 和主机名(实际的且不是由代理设置的),请使用以下命令:

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

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 %>
韬韬不绝 2024-09-15 19:45:58
String addr = request.getRemoteAddr();
String addr = request.getRemoteAddr();
_失温 2024-09-15 19:45:58
request.getHeader("X_FORWARDED_FOR") 
request.getHeader("X_FORWARDED_FOR") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文