定时刷新
<body>
<p>response自动刷新</p>
<%
response.setHeader("Refresh","10");
out.println(new Date());
%>
</body>
统计用户在某页停留时间
<%@page import="java.util.Date"%>运行结果:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
<%
session.setMaxInactiveInterval(11);//设置session的有效活动时间为11s
Date now = new Date();
int h=0;
int m=0;
int s=0;
if(session.isNew()){
session.setAttribute("start", now);
}else{
Date date = (Date)session.getAttribute("start");
Date end = new Date();
long howmuch =end.getTime()-(date.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);//计算在网站停留的秒
}
%>
<table width="500" height="100" border="1" bordercolor="black"cellspacing="0" style="margin-top: 200">
<tr bgcolor="lightgray" height="25">
<td align="center">
统计用户在某一页停留的时间
</td>
</tr>
<tr>
<td align="center">
<%--将用户登录的时间再页面中显示--%>
您登录的时间为:<%=((Date)session.getAttribute("start")).toLocaleString() %>
</td>
</tr>
<tr>
<td align="center">
您在本页的停留时间为:<%=h%>小时<%=m %>分<%=s %>秒
</td>
</tr>
</table>
</body>
</html>