jsp取客户端ip
直接用request.getRemoteAddr()我在服务端登进去显示的是0:0:0:0:0:0:0:1到底怎么样才能取得客户端的ip...
直接用request.getRemoteAddr() 我在服务端登进去显示的是0:0:0:0:0:0:0:1
到底怎么样才能取得客户端的ip 展开
到底怎么样才能取得客户端的ip 展开
3个回答
2013-08-30
展开全部
方法一:
public String getRemortIP(HttpServletRequest request)
{
if (request.getHeader("x-forwarded-for") == null)
{
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
方法二
public String getIpAddr(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.getRemoteAddr();
}
return ip;
}本文转载自 山东风创网络http://www.37net.net/html/2009-06-11/2823.html
public String getRemortIP(HttpServletRequest request)
{
if (request.getHeader("x-forwarded-for") == null)
{
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
方法二
public String getIpAddr(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.getRemoteAddr();
}
return ip;
}本文转载自 山东风创网络http://www.37net.net/html/2009-06-11/2823.html
2013-08-30
展开全部
首先,你要导入java.util.*;javax.servlet.http.*;javax.servlet.*;java.io.*这几个包。在使用request.getRemoteAddr() 来获取IP地址。在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。下面给你一段JAVA代码,是获取客户端真实IP的代码,测试通过 /**
* ip校验
* @param s
* @return
*/
public static Boolean isIpAddress(String s){
String regex = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
return m.matches();
} /**
* 获取客户端ip
* @param request
* @return
*/
public static String getClientAddress(HttpServletRequest request) {
String address = request.getHeader("X-Forwarded-For");
if (address != null && CheckFormat.isIpAddress(address)) {
return address;
}
return request.getRemoteAddr();
} 也可以使用下面的代码来实现String realIP = request.getHeader(''"x-forwarded-for''");
if (realIP != null && realIP.length() != 0 ){
while((realIP != null && realIP.equals(''"unknow''"))){
realIP = request.getHeader(''"x-forwarded-for''");
}
}
if (realIP == null || realIP.length() ==0 ){
realIP = request.getHeader(''"Proxy-Clint-IP''");
}
if (realIP == null || realIP.length() ==0 ){
realIP = request.getHeader(''"WL-Proxy-Clint-IP''");
}
if (realIP == null || realIP.length() == 0){
realIP = request.getRemoteAddr();
}
out.print(''"你的IP地址是:''"+realIP);
* ip校验
* @param s
* @return
*/
public static Boolean isIpAddress(String s){
String regex = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
return m.matches();
} /**
* 获取客户端ip
* @param request
* @return
*/
public static String getClientAddress(HttpServletRequest request) {
String address = request.getHeader("X-Forwarded-For");
if (address != null && CheckFormat.isIpAddress(address)) {
return address;
}
return request.getRemoteAddr();
} 也可以使用下面的代码来实现String realIP = request.getHeader(''"x-forwarded-for''");
if (realIP != null && realIP.length() != 0 ){
while((realIP != null && realIP.equals(''"unknow''"))){
realIP = request.getHeader(''"x-forwarded-for''");
}
}
if (realIP == null || realIP.length() ==0 ){
realIP = request.getHeader(''"Proxy-Clint-IP''");
}
if (realIP == null || realIP.length() ==0 ){
realIP = request.getHeader(''"WL-Proxy-Clint-IP''");
}
if (realIP == null || realIP.length() == 0){
realIP = request.getRemoteAddr();
}
out.print(''"你的IP地址是:''"+realIP);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-30
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询