现在的位置: 首页 > 综合 > 正文

【JSP】使用application统计所有用户对某网页的访问次数

2013年05月24日 ⁄ 综合 ⁄ 共 932字 ⁄ 字号 评论关闭

因为使用application对象完成累计的功能,所以当

(1)当前的Wen应用重新部署

(2)Tomcat服务器重启

计数器要重新开始计数。

jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	//判断application对象中有没有保存名为count的参数
	//如果没有,在application对象中新增一个名为count的参数
	if(application.getAttribute("count")==null){
		application.setAttribute("count", new Integer(0));
	}
	Integer count = (Integer)application.getAttribute("count");
	//使用application对象读取count参数的值,再在原值基础上累加1
	application.setAttribute("count",new Integer(count.intValue()+1));
%>
	<h2>
		<!-- 输出累加后的count参数对应的值 -->
		欢迎您访问,本页面已经被访问过 <font color="#ff0000"><%=application.getAttribute("count") %></font>次。。。。
	</h2>
</body>
</html>

在浏览器输入:http://localhost:8888/WebDemo/count.jsp

在需要计数的jsp文件中,包含该count.jsp即可。

	<%@include file="count.jsp" %>

抱歉!评论已关闭.