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;
}
}
在项目中部署直接运行就行了