后台访问记录
前台
<script type="text/javascript"> $(document).ready(function () { $.getJSON("/portal/visitor/updateVisitor?rn="+Math.random(),function(data){ if("success"==data.result){ //parent.location.href="/portal/home/index"; } else{ alert("logout failure!"); } }); }); </script>
后台处理
@RequestMapping("/updateVisitor") public void updateVisitor(HttpServletRequest request,HttpServletResponse response){ boolean flag=false; System.out.println(request.getRemoteHost()); System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())); System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(new Date())); List<Visitor> allVisitors = visitorService.findAll(); for(Visitor vis:allVisitors){ String ip=request.getRemoteHost(); if(request.getRemoteHost().equals(vis.getIp())){ flag=true; } } if(flag==false){ Visitor visitor = new Visitor(); visitor.setIp(request.getRemoteHost()); visitor.setName("***"); visitor.setVisitTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())); visitor.setVisitDate(new SimpleDateFormat("yyyy/MM/dd").format(new Date())); visitorService.save(visitor); } else{ visitorService.updateByIP(request.getRemoteHost(),new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()),new SimpleDateFormat("yyyy/MM/dd").format(new Date())); } Long totalToday = visitorService.todayVisitor(); request.getSession().setAttribute("total", totalToday); JSONObject data = new JSONObject(); try { data.put("result", "success"); } catch (Exception e) { System.out.println(e.getMessage()); } PrintWriter out = null; response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); try { out=response.getWriter(); out.write(data.toString()); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
todayVisitor函数
public Long todayVisitor() { SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); List<Visitor> list = getSession().createQuery("FROM Visitor v WHERE v.visitDate like'%"+df.format(new Date())+"%'").list(); Long count = Long.valueOf(list.size()); return count; }