Get IP address of client in JSP

前端 未结 4 697
慢半拍i
慢半拍i 2021-02-09 11:36

I need to get the IP address of the client in the JSP page. I have tried the following ways:

request.getRemoteAddr()
request.getHeader(\"X_FORWARDED_FOR\")
reque         


        
4条回答
  •  我寻月下人不归
    2021-02-09 12:20

    To get IP address of the client, I've used the following method

    <%   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();  
            }
            %>
    

    Hope this helps, please leave a feed back.

提交回复
热议问题