一個可以防止刷新的JSP計數(shù)器

2022-06-12發(fā)布者:ylm大?。?/span> 下載:0

文件大小:

軟件介紹

<%@ page contentType="text/html;charset=gb2312" %>
<!--jsp計數(shù)器-->
<%-- 以前學(xué)asp時,用ASP做防止刷新的計數(shù)器很簡單,以下是一個用JSP做的計數(shù)器--%>
<html>
<head>
<title>jsp計數(shù)器</title>
</head>
<body>
<%@ page import="java.io.*" %>
<%
//out.PRintln(request.getHeader("Cookie"));
String currentRecord = null;//保存文本的變量
BufferedReader file; //BufferedReader對象,用于讀取文件數(shù)據(jù)
String nameOfTextFile = "count.txt";

//讀取
file = new BufferedReader(new FileReader(nameOfTextFile));
String readStr =null;
int writeStr =0; //如果計數(shù)文本中的計數(shù)值為空則讓它顯示時變成1并寫入
try
{ readStr = file.readLine(); }
catch (IOException e)
{ System.out.println("讀取數(shù)據(jù)錯誤."); }
if (readStr == null) readStr = "沒有任何記錄";

//判斷cookie,第一次登陸時加1,刷新時不累計計數(shù)
else if (request.getHeader("Cookie")==null)
{ writeStr = Integer.parseInt(readStr)+1;}
else
{ writeStr = Integer.parseInt(readStr);}

//寫入時控制因為刷新引起的重復(fù)計數(shù)
if (request.getHeader("Cookie")==null)
{
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(writeStr);
pw.close();}
catch(IOException e) {
out.println(e.getMessage());}
}
%>
<p align="center">您是CNJSP的第<b><font color="red"><%=writeStr%></font></b>位客人。</p>
</body>
</html>

發(fā)表評論(共0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)政策法規(guī),評論內(nèi)容只代表網(wǎng)友觀點,發(fā)表審核后顯示!

版權(quán)聲明:

1 本站所有資源(含游戲)均是軟件作者、開發(fā)商投稿,任何涉及商業(yè)盈利目的均不得使用,否則產(chǎn)生的一切后果將由您自己承擔(dān)!

2 本站將不對任何資源負(fù)法律責(zé)任,所有資源請在下載后24小時內(nèi)刪除。

3 若有關(guān)在線投稿、無法下載等問題,請與本站客服人員聯(lián)系。

4 如侵犯了您的版權(quán)、商標(biāo)等,請立刻聯(lián)系我們并具體說明情況后,本站將盡快處理刪除,聯(lián)系QQ:2499894784

返回頂部