2011年11月1日 17:36:42 开始 以下代码仅供参考
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
int i = 1;
%>
<%
Object count = application.getAttribute("count");
Object count1 = application.getAttribute("count1");
Object count2 = application.getAttribute("count2");
%>
<%
if (count == null) {
application.setAttribute("count", i);
%>
<%
} else {
application.setAttribute("count", (Integer) (count) + 1);
}
%>
首页访问次数:<%=count%>
<h1><a href="index2.jsp">第一页面</a></h1><h2>访问量</h2><%=count1 %><br/>
<h1><a href="index3.jsp">第二页面</a></h1><h2>访问量</h2><%=count2 %>
index2,jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
int j = 1;
%>
<%
Object count = application.getAttribute("count");
Object count1 = application.getAttribute("count1");
%>
<%
if (count1 == null) {
application.setAttribute("count1", j);
%>
<%
} else {
application.setAttribute("count1", (Integer) (count1) + 1);
application.setAttribute("count", (Integer) (count) + 1);
}
%>
分页一访问次数:<%=count1%><br/>
网站访问量:<%=count%>
index3.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
int k = 1;
%>
<%
Object count = application.getAttribute("count");
Object count2 = application.getAttribute("count2");
%>
<%
if (count2 == null) {
application.setAttribute("count2", k);
%>
<%
} else {
application.setAttribute("count2", (Integer) (count2) + 1);
application.setAttribute("count", (Integer) (count) + 1);
}
%>
分页二访问次数:<%=count2%>
网站访问量:<%=count%>
2011年11月1日 17:37:04 结束 以上代码仅供参考
本文提供了一组使用JSP实现网站不同页面访问计数的示例代码。通过将计数器保存在应用程序范围内,可以跟踪首页及各分页的访问次数。
1万+

被折叠的 条评论
为什么被折叠?



