JSP簡單練習-網站計數器,jsp練習計數器

來源:互聯網
上載者:User

JSP簡單練習-網站計數器,jsp練習計數器

<%@ page contentType="text/html;charset=gb2312" %><%@ page import="javax.servlet.*" %><html><head><title>網站計數器</title></head><body>   <%!      synchronized void countPeople()      {  // 序列化計數函數         ServletContext application=((HttpServlet)(this)).getServletContext();         Integer number=(Integer)application.getAttribute("Count");         if(number==null)         {  // 如果是第1個訪問本站            number=new Integer(1);            application.setAttribute("Count", number);         }         else         {            number=new Integer(number.intValue()+1);            application.setAttribute("Count",number);         }      }   %>   <%      if(session.isNew())      {  // 如果是一個新的會話         out.println("是一個新會話!");         countPeople();      }      Integer yourNumber=(Integer)application.getAttribute("Count");      out.println(yourNumber);   %>   <p>歡迎訪問本站,你是第      <%=yourNumber %>           個訪問使用者。</body></html>

程式利用synchronize關鍵字對計數函數進行了序列化(有的書中叫序列化),以確保當兩個用戶端同時訪問網頁而修改計數值時不會產生衝突;getServletContext()方法來得到application對象,因為有些Web伺服器並不直接支援application對象,必須先得到其上下文;如果還是第一個訪問的客戶,則前面代碼中得到的number會是空值,故置初始值為1,否則做增1處理;如果是一個新的會話則調用計數函數,得到計數值並將其顯示。

可以發現,當重新整理頁面時,其數值並不會增加,只有關閉了本網站的所有視窗再重新訪問時,才會增1,因為這又是一個新的會話。



jsp 網頁計數器的簡單問題

那肯定不行了,因為你web伺服器(tomacat或者apache)解析的範圍不包括你的圖片位置所在。最簡單的辦法就是,在項目根目錄建立一個存放圖片的檔案夾比如:images,然後將你計數所用的圖片都放在這個檔案夾下就行了。
然後修改成: out.print("<img src="images/"+number.charAt(i)+".GIF'>");即可。
 
怎為jsp網站添加計數器

一個使用TXT文本的JSP網站訪問計數器(不用任何資料庫)

//counter.java 讀寫檔案的一個bean
===========================
//網站讀寫txt格式計數器
package net.com.util;

import java.io.*;

public class Counter extends Object {
private String currentRecord = null;//儲存文本的變數
private BufferedReader file; //BufferedReader對象,用於讀取檔案資料
private String path;//檔案完整路徑名
public Counter() {
}
// ReadFile方法用來讀取檔案filePath中的資料,並返回這個資料
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
// 建立新的BufferedReader對象
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try
{
// 讀取一行資料並儲存到currentRecord變數中
currentRecord = file.readLine();
}
catch (IOException e)
{//錯誤處理
System.out.println("讀取資料錯誤.");
}
if (currentRecord == null)
// 如果檔案為空白
returnStr = "沒有任何記錄";
else
{//檔案不為空白
returnStr =currentRecord;
}
// 返回讀取檔案的資料
return returnStr;
}
// ReadFile方法用來將資料counter+1後寫入到文字檔filePath中
// 以實現計數增長的功能
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath;
// 將counter轉換為int類型並加一
int Writestr = Integer.parseInt(counter)+1;
try {
// 建立PrintWriter對象,用於寫入資料到檔案中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
// 用文字格式設定列印整數Writestr
pw.println(Writestr);
// 清除PrintWriter對象
pw.close();
} catch(IOException e) {
// 錯誤處理
System.out.println("寫入檔案錯誤"+e.getMessage());
}
}

}

......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.