Java 如何限制JSP命中计数器?
我正在用JSP编写一个命中计数器,用于我的课程作业。我已经编写了代码,没有错误,也没有工作,但问题是: 如果用户已打开网站并尝试使用不同的页面,每当用户返回主页时,计数器仍在添加数字,我如何限制此部分?我们应该用会话来限制它吗? 这是我的代码:Java 如何限制JSP命中计数器?,java,jsp,hitcounter,Java,Jsp,Hitcounter,我正在用JSP编写一个命中计数器,用于我的课程作业。我已经编写了代码,没有错误,也没有工作,但问题是: 如果用户已打开网站并尝试使用不同的页面,每当用户返回主页时,计数器仍在添加数字,我如何限制此部分?我们应该用会话来限制它吗? 这是我的代码: <jsp:useBean id="counter" scope="application" class="counter.CounterBean" /> The current count for the counter bean is: &
<jsp:useBean id="counter" scope="application" class="counter.CounterBean" />
The current count for the counter bean is:
<jsp:setProperty name="counter" property="coun" value="1"></jsp:setProperty>
<%
counter.saveCount();
int _numberofvisitors=counter.getVisitorsNumber();
out.println(_numberofvisitors);
%>
更改代码的这一部分:
<%
counter.saveCount();
int _numberofvisitors=counter.getVisitorsNumber();
out.println(_numberofvisitors);
%>
JSP中的application对象
访问总数:
不清楚您想要计算什么:无论访问的页面是什么,每次都有1个唯一的访问者?您想根据会话或唯一IP来计算吗?还是每次都要计算每个访问者的唯一页面浏览量?等
<%
counter.saveCount();
int _numberofvisitors=counter.getVisitorsNumber();
out.println(_numberofvisitors);
%>
<%
if (session.isNew()) {
counter.saveCount();
} else {
counter.setCoun(-1);
}
int _numberofvisitors=counter.getVisitorsNumber();
out.println(_numberofvisitors);
%>
<jsp:setProperty name="counter" property="coun" value="${1 + counter.coun}"></jsp:setProperty>
<%@page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Applcation object in JSP</title>
</head>
<body>
<%
Integer hitsCount=(Integer)application.getAttribute("hitcount");
int m;
if(hitsCount==null)
{
m=1;
hitsCount =Integer.valueOf(m);
application.setAttribute("hitcount", hitsCount);
}
else
{
hitsCount=(Integer)application.getAttribute("hitcount");
m=hitsCount.intValue()+1;
hitsCount= Integer.valueOf(m);
application.setAttribute("hitcount", hitsCount);
}
%>
<center>
<p>Total number of visits:<%=hitsCount.intValue()%> </p>
</center>
</body>
</html>