Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jsp页面中显示客户端ip?_Java_Linux_Jsp - Fatal编程技术网

Java 如何在jsp页面中显示客户端ip?

Java 如何在jsp页面中显示客户端ip?,java,linux,jsp,Java,Linux,Jsp,我正在尝试获取系统的ip,该系统正在查看我的jsp页面。当客户端系统是windows时,它可以正常工作。但在linux中它不工作。我使用以下代码来完成此操作。有人能建议我解决我的问题吗 代码: Mac.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01

我正在尝试获取系统的ip,该系统正在查看我的jsp页面。当客户端系统是windows时,它可以正常工作。但在linux中它不工作。我使用以下代码来完成此操作。有人能建议我解决我的问题吗

代码:

Mac.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="mc" method="post">
<input type="submit" value="Send">
 <p>Your IP address is: ${pageContext.request.remoteAddr}</p> 
<% String ipAddress = request.getHeader("X-FORWARDED-FOR");
String is="";
if (ipAddress == null) {
    is = request.getRemoteAddr();
}
String OS="";
String userAgent = request.getHeader("User-Agent");
if((userAgent.toLowerCase().indexOf("windows") >= 0)){
    OS="windows";
}
else if((userAgent.toLowerCase().indexOf("linux") >= 0)){
    OS="linux";
}

%>


<p>address:<%=is%>  <%=userAgent%></p>
<input type="text" value="<%=is%>" name="ip">
<input type="text" value="<%=OS%>" name="os">
</form>

</body>
</html>
输出 您的IP地址是:127.0.0.1

地址:127.0.0.1 Mozilla/5.0(X11;Ubuntu;Linux i686;rv:36.0)Gecko/20100101 Firefox/36.0


如何在jsp页面中显示linux系统ip根据注释中的讨论,服务器和客户端都在同一个系统中

localhost的IP地址是127.0.0.1


因此,您得到的输出是127.0.0.1

从各种头文件中,您可以获得客户机的IP地址,试试看。尝试检查以上所有标题

    public static String getClientIpAddr(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("Proxy-Client-IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("WL-Proxy-Client-IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("HTTP_CLIENT_IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getRemoteAddr();

            return ip;
        }

本地主机的ip地址是127.0.0.1您的linux系统是否托管您的web应用程序?@SpringLearner是的,我的linux系统已托管应用程序。您如何访问jsp。。还是@BigMike客户端通过使用我的ip访问我的jsp,比如192.168.1.145:8080/checkingfiles/mac.jsptry从另一台机器访问你的jsp,你看到了什么?你是说如果有其他linux客户端访问我的jsp就意味着它工作正常吗?@KVK我没有发现你的代码有任何错误,我的回答描述了为什么你会得到127.0.0.1
    public static String getClientIpAddr(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("Proxy-Client-IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("WL-Proxy-Client-IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("HTTP_CLIENT_IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getRemoteAddr();

            return ip;
        }