网页获取访问IP

<%@ 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>首页</title>
</head>
<%@ page import="cn.personweb.Dao.*" %>
<%!
//获取访问ip的方法
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;    
}
%>
<%
	//使用方法获取ip
	String ip=getIpAddr(request);
	//将获取的ip添加到数据库
	ipDao.insertip("P", ip);
%>
<body>
	<jsp:forward page="personweb/index.jsp"></jsp:forward>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/yuan52007298/article/details/80206641