用application实现一个网页的浏览计数器

<%@ 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>
<%
Object count = application.getAttribute("count") ;

if(count==null)//首先判断是否存在该全局变量
{
    //创建全局属性
    application.setAttribute("count",1) ;
}
else
{
    int co = Integer.parseInt(count.toString()) ; //定义变量并赋值
    
    out.print(co++) ;
    
    //写会
    application.setAttribute("count", co) ;
}
%>
<a href="application2.jsp" >访问</a>
</body>
</html>
<%@ 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属性
int count = Integer.parseInt(application.getAttribute("count").toString()) ;

out.print("网站计数器:"+count++) ;
application.setAttribute("count", count) ;
%>
</body>
</html>

 

posted @ 2016-12-21 21:18  -加勒比海带  阅读(2571)  评论(0编辑  收藏  举报