用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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM