统计用户在站点停留的时间并在另一个网页中查看(JSP+mysql)

一.思路:

1.首先我们要有一个连接数据库的DB类
2.其次我们需要一个时间计算类
3.然后我们需要一个监听类
4.一个用户登入jsp页面
5.一个后台查看jsp页面

代码如下:
1.DB.java

package exa114;

import java.sql.*;

public class DB {

	private Connection con;
	private Statement stm;
	private ResultSet rs;
	private String url="jdbc:mysql://localhost:3306/jdbc?&useSSL=false&serverTimezone=UTC";
	private String classname="com.mysql.cj.jdbc.Driver";
	
	   public Connection getCon()//连接数据库方法,返回数据库对象
	   {
		   try{
			   Class.forName(classname);
			   
		   }catch(ClassNotFoundException e)
		   {
			   e.printStackTrace();
		   }
		   try{
			   con=DriverManager.getConnection(url,"root","root");
		   }catch(Exception e)
		   {
			   e.printStackTrace();
			   con=null;
		   }
		   return con;
		   
	   }
	   
	   public Statement getStmed()//返回sql语句执行对象
	   {
		   try{
			   con=getCon();
			   stm=con.createStatement();
			   
		   }catch(Exception e){
			   e.printStackTrace(System.err);
		   }
		   return stm;
	   }
	   
	   public ResultSet search(String sql)//返回查询集对象
	   {
		   stm=getStmed();
		   try{
			   rs=stm.executeQuery(sql);
			   
		   }catch(Exception e)
		   {
			   e.printStackTrace();
		   }
		   return rs;
	   }
	   
	   public int dosql(String sql)//返回语句执行结果
	   {
		   int i=-1;
		   stm=getStmed();
		   try{
			   i=stm.executeUpdate(sql);
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		   return i;
	   }

	   public void closed()//关闭数据库
	   {
		   try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }
}

2.时间计算类:
StopTime1.java

package exa114;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StopTime1 {

	private int h=0;
	private int m=0;
	private int s=0;
	private Date start=null;
	private Date end=null;
	private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	public void setStart(Date start)
	{
		this.start=start;
	
	}
	public void conuttime(Date end)
	{
	    this.end=end;
		long howmuch=end.getTime()-start.getTime();
		h=(int)(howmuch/1000/60/60);//小时
		howmuch=howmuch-h*60*60*1000;
		m=(int)(howmuch/1000/60);//分钟
		howmuch=howmuch-m*60*1000;
		s=(int)(howmuch/1000);//秒
	}

	public String getTimes()
	{
		String times=this.h+"小时"+this.m+"分"+this.s+"秒";
		return times;
	}
	
	public String getStart()
	{
		return format.format(start);
	}
	public String getEnd()
	{
		return format.format(end);
	}
}

3.会话监听类:
HttpSessionBindingListener内有两个方法valueBound(HttpSessionBindingEvent event)和valueUnbound(HttpSessionBindingEvent event),前者为数据绑定,后者为取消绑定
session进行数据绑定,就是调用session.setAttribute()把HttpSessionBindingListener保存进session中。

package exa114;

import java.util.Date;

import javax.servlet.http.HttpSessionBindingEvent;

public class UserLine implements javax.servlet.http.HttpSessionBindingListener {
	private String userip;
	private StopTime1 stoptime=new StopTime1();
	
	public UserLine()
	{
		userip="";
	}
	public void setUserip(String userip)
	{
		this.userip=userip;
	}
	public String getUserip()
	{
		return userip;
	}
	
	public void valueBound(HttpSessionBindingEvent arg0)
	{
		stoptime.setStart(new Date());
		System.out.println(this.userip+"于"+new Date().toLocaleString()+"上线");
	}
	
	public void valueUnbound(HttpSessionBindingEvent arg0)
	{
		stoptime.conuttime(new Date());
		System.out.println(this.userip+"于"+new Date().toLocaleString()+"下线");
		writetime();
	}
	public void writetime()//写入操作
	{
		String starttime=stoptime.getStart();
		String endtime=stoptime.getEnd();
		String times=stoptime.getTimes();
		String sql="insert into tb_stoptime values('"+this.userip+"','"+starttime+"','"+endtime+"','"+times+"')";
		DB db=new DB();
		db.dosql(sql);
		db.closed();
	}

	
	
}

4.用户页面;
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <%@page import="exa114.*" %>
<!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>Insert title here</title>
</head>
<body>

<%
session.setMaxInactiveInterval(10);//设置session有效活动时间
String userip=request.getRemoteAddr();
UserLine userline=new UserLine();
userline.setUserip(userip);
session.setAttribute("logonuser",userline);
%>
<table>
  <tr bgcolor="lightgrey" height="25">
    <td align="center">欢迎登录!</td>
  </tr>
  
</table>
</body>
</html>

5.后台查看jsp页面:
test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@page import="java.sql.*" %>
     <jsp:useBean id="mydb" class="exa114.DB"></jsp:useBean>
<!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>Insert title here</title>
</head>
<body>
<%


String sql="select * from tb_stoptime";
ResultSet rs=mydb.search(sql);



%>

<table>
 <tr bgcolor="lightgrey">
   <td align="center">userip</td>
   <td align="center">user_ontime</td>
   <td align="center">user_offtime</td>
    <td align="center">user_stoptime</td>
 </tr>
 <%while(rs.next()){ %>
<tr>
<td align="center"><%=rs.getString("userip") %></td>
<td align="center"><%=rs.getString("user_ontime") %></td>
<td align="center"><%=rs.getString("user_offtime") %></td>
<td align="center"><%=rs.getString("user_stoptime") %></td>
</tr>

<%} 
 mydb.closed();
%>


</table>

</body>
</html>

结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小牧之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值