StopTime.class
import java.util.Date;
public class StopTime {
private int h =0;
private int m=0;
private int s=0;
public StopTime(){}
public void counttime(Date start){
Date end = new Date();
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 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;
}
}
index.jsp
<%@ page import="java.util.*" %>
<jsp:useBean id="mycounttime" class="com.count.StopTime" scope="page"></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">
<meta http-equiv="refresh" content="10"><!-- 设置刷新时间 -->
<title>Insert title here</title>
</head>
<body>
<%
session.setMaxInactiveInterval(11);//设置session的有效活动时间为11s
Date now = new Date();
if(session.isNew()){
session.setAttribute("start", now);
}else{
mycounttime.counttime((Date)session.getAttribute("start"));
}
%>
<table>
<tr>
<td align="center">
您登陆的时间为:<%=((Date)session.getAttribute("start")).toLocaleString() %>
</td>
</tr>
<tr>
<td align="center">
您在本页停留的时间为:<%=mycounttime.getH() %>小时<%=mycounttime.getM() %>分钟<%=mycounttime.getS() %>秒。
</td>
</tr>
</table>
</body>
网友评论