通过sessio对象保存用户访问页面的次数,设置session对象保存数据的有效时间。求解答。在线等

2025-04-07 13:34:59
推荐回答(2个)
回答1:

主要实现单个页面的访问次数记录与总访问次数记录
///////////////////////index.jsp/////////////////////////////////
主页面


<%
Integer count = (Integer)application.getAttribute("count");
if(count==null){
count=0;
}
%>

<%
Integer count1 = (Integer)application.getAttribute("count1");
if(count1==null){
count1=0;
}
%>
总访问量:
<%=count+count1 %>

///////////////////////////////////1.jsp//////////////////////////////////
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
Integer count = (Integer)application.getAttribute("count");
if(count==null){
count=1;
application.setAttribute("count",count);
}else{
count++;
application.setAttribute("count",count);
}
%>
访问人数为:
<%=count %>
返回首页
///////////////////////////////////2.jsp///////////////////////////////////////
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
Integer count1 = (Integer)application.getAttribute("count1");
if(count1==null){
count1=1;
application.setAttribute("count1",count1);
}else{
count1++;
application.setAttribute("count1",count1);
}
%>
访问人数为:
<%=count1 %>
返回首页

回答2:

session的超时时间在web.xml中设定的。

想要记录用户访问页面的次数可以做一个TAG,放在画面里。或者使用filter.