java之访问日志

2/10/2017来源:ASP.NET技巧人气:313

需求:

别人访问你的网站,你想知道谁访问你,在哪里访问的,最好的办法是站长之家,可是你想自己写,于是就写个小小的例子,以供学习!

分析:

先从后端获取ip,然后前端ip通过腾讯地图获取省市地址,然后再通过后端保存端口、浏览器信息等资料。

主页面:

<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.5.1/jquery.min.js"></script> <script charset="utf-8" src="http://map.QQ.com/api/js?v=2.exp"></script> <script type="text/Javascript" src="static/visit.net-1.0.js"></script>

visit.net-1.0.js:

window.onload = function(){ $.Ajax( { type : "POST", url : "/sshTest/index/getVisitIp.do", timeout : 20000, cache : false, success : function(data, textStatus) { var ipaddre = data; var geocoder,citylocation,map,marker,locationLat,addresses,PRovince,city,address,ipaddress,ipaddre2 = null; function insertQR(province,city,addresses,locationLat,ipaddre2){ var llac = "'"+locationLat+"'"; alert(llac) $.ajax( { type : "POST", url : "/sshTest/index/visitNet5.do", data : {"province":province, "city":city, "addresses":addresses, "lolat":llac, "ip":ipaddre2 }, cache : false, success : function(data, textStatus) {} }); } var center = new qq.maps.LatLng(39.916527,116.397128); map = new qq.maps.Map(document.getElementById('container'),{ center: center, zoom: 13 }); geocoder = new qq.maps.Geocoder(); citylocation = new qq.maps.CityService({ map : map, complete : function(results){ codeLatLng(results.detail.latLng); } }); function codeLatLng(latLng) { var info = new qq.maps.InfoWindow({map: map}); geocoder.getAddress(latLng); geocoder.setComplete(function(result) { map.setCenter(result.detail.location); locationLat = result.detail.location; addresses = result.detail.address; province=result.detail.addressComponents.province; city=result.detail.addressComponents.city; ipaddre2 = ipaddre; insertQR(province,city,addresses,locationLat,ipaddre2); }); geocoder.setError(function() {}); } var ipaddress = data; citylocation.searchCityByIP(ipaddress); } }); }

controller

@RequestMapping("getVisitIp") @ResponseBody public String getVisitIp(HttpServletRequest request) { // String ip = _ipUtil.getRemoteHost(request); String ip = "14.147.40.76"; return ip; } /** * 访问日志 * @param province 省 * @param city 市 * @param addresses 详细地址 * @param locationLat 经纬度 * @param ip ip地址 * @param request * @return */ @RequestMapping("visitNet5") @ResponseBody public void visitNet5(String lolat,Visitnet net,HttpServletRequest request) { net.setLocationlat(lolat.replaceAll("'", "")); int port = request.getRemotePort(); // TCP/IP端口 String agent = request.getHeader("user-agent"); //浏览器信息 Date date = new Date(); //访问时间 net.setPort(port+""); net.setAgent(agent); net.setCreatetime(date); System.out.println(port+agent); }

ipUtil

public static String getRemoteHost(javax.servlet.http.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.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip; }

数据库

CREATE TABLE `visitnet` ( `id` int(11) NOT NULL auto_increment, `province` varchar(255) default NULL, `city` varchar(255) default NULL, `addresses` varchar(255) default '' COMMENT '详细地址', `locationLat` varchar(255) default '' COMMENT '经纬度', `ip` varchar(255) default '' COMMENT 'ip地址', `port` varchar(255) default NULL, `agent` varchar(255) default NULL, `createTime` datetime default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

截图留恋

这里写图片描述

全CSDN的丰(好)色(se)博客,这里有书本看不到的Java技术,电影院禁播的电影,欢迎关注QQ群494808400