`
358713428
  • 浏览: 23917 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

计数用户在页面上停留时间

阅读更多

jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<jsp:useBean id="my" class="hs.times.StopTime" scope="page"></jsp:useBean>
<%
	session.setMaxInactiveInterval(11);
	Date now =new Date();
	if(session.isNew()){
		session.setAttribute("start",now);
	}else{
		my.countTime((Date)session.getAttribute("start"));
	}
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="refresh" content="100"><!-- 设置页面自动刷新时间-->
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  	
  <body>
    	<table>
    		<tr>
    			<td align="center">您登陆的时间是:<%=((Date)session.getAttribute("start")).toLocaleString()%></td>
    		</tr>
    		<tr>
    			<td align="center">
    				您在本页停留时间为:<%=my.getH() %>小时<%=my.getM() %>分钟<%=my.getS() %>秒
    			</td>
    		</tr>
    	</table>
  </body>
</html>
 

 

java

package hs.times;

import java.util.Date;

public class StopTime {
	private int h=0;
	private int m=0;
	private int s=0;
	
	public void countTime(Date date){
		Date da =new Date();
		long much=da.getTime()-date.getTime();
		h=(int)(much/1000/60/60);
		much =much-h*60*60*1000;
		m=(int)(much/1000/60);
		much=much-m*60*1000;
		s=(int)(much/1000);
	}
	
	public int getH() {
		return h;
	}
	public void setH(int h) {
		this.h = h;
	}
	public int getM() {
		return m;
	}
	public void setM(int m) {
		this.m = m;
	}
	public int getS() {
		return s;
	}
	public void setS(int s) {
		this.s = s;
	}
}

 

在项目中部署直接运行就行了

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics