How do I get the remote address of a client in servlet?

前端 未结 10 2112
刺人心
刺人心 2020-11-22 07:32

Is there any way that I could get the original IP address of the client coming to the server? I can use request.getRemoteAddr(), but I always seem to get the IP

10条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 08:20

    String ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null) {
                ipAddress = request.getHeader("X_FORWARDED_FOR");
                if (ipAddress == null){
                    ipAddress = request.getRemoteAddr();
                }
            }
    

提交回复
热议问题