• 后台访问记录


    前台

    <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;
    }
    
  • 相关阅读:
    C#.NET常见问题(FAQ)-如何在不同窗体之间传递值
    C#.NET常见问题(FAQ)-如何不显示窗口的关闭按钮
    C#.NET常见问题(FAQ)-如何判断两个类是否相同类型
    C#.NET常见问题(FAQ)-如何判断某个字符是否为汉字
    C#.NET常见问题(FAQ)-如何改变字符串编码
    C# 多线程编程 ThreadStart ParameterizedThreadStart
    C# 线程调用主线程中的控件
    LINQ to XML 编程基础
    LINQ to XML 建立,读取,增,删,改
    WinForm 自动完成控件实例代码简析
  • 原文地址:https://www.cnblogs.com/wujixing/p/5952124.html
Copyright © 2020-2023  润新知