jsp利用application統計在線人數的方法
更新時間:2015年01月05日 11:01:18 投稿:shichen2014
這篇文章主要介紹了jsp利用application統計在線人數的方法,代碼中備有較為詳盡的注釋便于理解,是比較實用的技巧,需要的朋友可以參考下
本文實例講述了jsp利用application統計在線人數的方法。分享給大家供大家參考。
具體實現方法如下:
復制代碼 代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>application</title>
</head>
<body>
<%!
Integer number;//
synchronized void numberVisiter()
{
ServletContext application = getServletContext();
Integer num = (Integer)application.getAttribute("count");
if(num == null)//如果是第一個訪問者
{
num = new Integer(1);
application.setAttribute("count",num);
}
else
{
num = new Integer(num.intValue() + 1);
application.setAttribute("count",num);
}
}
%>
<%
if(session.isNew())
{
numberVisiter();
Integer number = (Integer)application.getAttribute("count");
}
%>
<p>
<font size="2" color="blue">簡單的頁面訪問計數器</font>
</p>
<p>
<font size="2" color="#000000">
歡迎訪問此頁面,您是<%=number%>個訪問用戶
</font>
</p>
</body>
</html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>application</title>
</head>
<body>
<%!
Integer number;//
synchronized void numberVisiter()
{
ServletContext application = getServletContext();
Integer num = (Integer)application.getAttribute("count");
if(num == null)//如果是第一個訪問者
{
num = new Integer(1);
application.setAttribute("count",num);
}
else
{
num = new Integer(num.intValue() + 1);
application.setAttribute("count",num);
}
}
%>
<%
if(session.isNew())
{
numberVisiter();
Integer number = (Integer)application.getAttribute("count");
}
%>
<p>
<font size="2" color="blue">簡單的頁面訪問計數器</font>
</p>
<p>
<font size="2" color="#000000">
歡迎訪問此頁面,您是<%=number%>個訪問用戶
</font>
</p>
</body>
</html>
希望本文所述對大家的jsp程序設計有所幫助。
相關文章
JBuilder2005實戰(zhàn)JSP之切換控制 圖文步驟
由于在login.jsp的表單中通過action屬性指定switch.jsp為響應的JSP文件,當用戶在login.jsp中選定登錄用戶,輸入密碼提交表單后,客戶端將向服務器發(fā)送一個HTTP請求,服務器即調用switch.jsp來響應這個請求2012-08-08
JSP連接MySql/MS SQL Server/Oracle數據庫連接方法[整理]
這個經常整理更新2008-10-10

