练习--JSP定时刷新与统计用户在某页停留时间

本文介绍了如何在JSP中实现页面的定时刷新,并探讨了如何统计用户在特定页面上的停留时间,为提升用户体验和数据分析提供基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定时刷新

<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>
运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值