??? 本實例實現(xiàn)一個簡單的網(wǎng)站計數(shù)器
???
[編程要點]???
這里主要用到了Java2的同步處理機制,每個Java application 都有basic.counter的屬性,當(dāng)application被調(diào)用一次就把basic.counter值加1.
??
?[程序?qū)崿F(xiàn)]
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%
? Integer count = null;
? //同步處理
? synchronized (application)
? {
??? //從內(nèi)存當(dāng)中讀取訪問量
??? count = (Integer) application.getAttribute("basic.counter");
??? if (count == null)
??? count = new Integer(0);
??? count = new Integer(count.intValue() + 1);
??? //將訪問量保存到內(nèi)存當(dāng)中
??? application.setAttribute("basic.counter", count);
? }
%>?
<html>
<head>
<title>簡單計數(shù)器</title>
</head>
<body>
<center>
<font size=10 color=blue>簡單計數(shù)器</font>
<br>
<hr>
<br>
<font size=5 color=blue>您好!您是本站的第 <%= count %> 位客人</font>
</center>
</body>
</html>
[運行結(jié)果]
posted on 2006-08-21 12:16
摩西 閱讀(1075)
評論(0) 編輯 收藏