gv天堂gv无码男同在线,欧美视频你懂的,毛片一级毛片毛片一级一级毛毛片,亚洲黄色视频免费播放,满18岁免费看的尤物视频,日本欧美三级片免费看,亚洲综合伊人影视在线播放

  • 首 頁(yè)
  • 采購(gòu)市場(chǎng)
  • 企業(yè)查詢(xún)
  • 營(yíng)銷(xiāo)建站
  • 營(yíng)銷(xiāo)推廣
  • 行業(yè)資訊
  • 發(fā)布信息
  • 網(wǎng)頁(yè)中的計(jì)數(shù)器問(wèn)題

    懸賞分:20|
    如果要統(tǒng)計(jì)網(wǎng)頁(yè)的在線(xiàn)人數(shù)和登陸人數(shù),如何在servlet中實(shí)現(xiàn)呢?
    知識(shí)庫(kù)標(biāo)簽: 計(jì)數(shù)器   |列兵
    其中我們用到了兩個(gè)文件,test.jsp文件用于在瀏覽器中運(yùn)行,counter.java是后臺(tái)的一個(gè)小java bean程序,用來(lái)讀計(jì)數(shù)器的值和寫(xiě)入計(jì)數(shù)器的值。而對(duì)于計(jì)數(shù)器的保存,我們采用了一個(gè)文本文件lyfcount.txt。
    下面是詳細(xì)的程序代碼(test.jsp放到web目錄下,counter.java放到class目錄):
    //test.jsp文件
    $#@60;%@ page contentType="text/html;charset=gb2312"%$#@62;
    $#@60;HTML$#@62;
    $#@60;HEAD$#@62;
    $#@60;meta http-equiv="Content-Type" content="text/html; charset=gb2312"$#@62;
    $#@60;META NAME="GENERATOR" CONTENT="Oracle JDeveloper"$#@62;
    $#@60;TITLE$#@62;
    計(jì)數(shù)器演示程序
    $#@60;/TITLE$#@62;
    $#@60;/HEAD$#@62;
    $#@60;BODY$#@62;
    $#@60;!--創(chuàng)建并調(diào)用bean(counter)--$#@62;
    $#@60;jsp:useBean id="counter" class="counter" scope="request"$#@62;
    $#@60;/jsp:useBean$#@62;
    $#@60;%
    //調(diào)用counter對(duì)象的ReadFile方法來(lái)讀取文件lyfcount.txt中的計(jì)數(shù)
    String cont=counter.ReadFile("/lyfcount.txt");
    //調(diào)用counter對(duì)象的ReadFile方法來(lái)將計(jì)數(shù)器加一后寫(xiě)入到文件lyfcount.txt中
    counter.WriteFile("/lyfcount.txt",cont);%$#@62;
    您是第$#@60;font color="red"$#@62;$#@60;%=cont%$#@62;$#@60;/font$#@62;位訪(fǎng)問(wèn)者
    $#@60;/BODY$#@62;
    $#@60;/HTML$#@62;
    //counter.java 讀寫(xiě)文件的一個(gè)bean
    import java.io.*;
    public class counter extends Object {
    private String currentRecord = null;//保存文本的變量
    private BufferedReader file; //BufferedReader對(duì)象,用于讀取文件數(shù)據(jù)
    private String path;//文件完整路徑名
    public counter() {
    }
    //ReadFile方法用來(lái)讀取文件filePath中的數(shù)據(jù),并返回這個(gè)數(shù)據(jù)
    public String ReadFile(String filePath) throws FileNotFoundException
    {
    path = filePath;
    //創(chuàng)建新的BufferedReader對(duì)象
    file = new BufferedReader(new FileReader(pa );
    String returnStr =null;
    try
    {
    //讀取一行數(shù)據(jù)并保存到currentRecord變量中
    currentRecord = file.readLine();
    }
    catch (IOException e)
    {//錯(cuò)誤處理
    System.out.println("讀取數(shù)據(jù)錯(cuò)誤.");
    }
    if (currentRecord == null)
    //如果文件為空
    returnStr = "沒(méi)有任何記錄";
    else
    {//文件不為空
    returnStr =currentRecord;
    }
    //返回讀取文件的數(shù)據(jù)
    return returnStr;
    }
    //ReadFile方法用來(lái)將數(shù)據(jù)counter+1后寫(xiě)入到文本文件filePath中
    //以實(shí)現(xiàn)計(jì)數(shù)增長(zhǎng)的功能
    public void WriteFile(String filePath,String counter) throws FileNotFoundException
    {
    path = filePath;
    //將counter轉(zhuǎn)換為int類(lèi)型并加一
    int Writestr = Integer.parseInt(counter)+1;
    try {
    //創(chuàng)建PrintWriter對(duì)象,用于寫(xiě)入數(shù)據(jù)到文件中
    PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
    //用文本格式打印整數(shù)Writestr
    pw.println(Writestr);
    //清除PrintWriter對(duì)象
    pw.close();
    } catch(IOException e) {
    //錯(cuò)誤處理
    System.out.println("寫(xiě)入文件錯(cuò)誤"+e.getMessage());
    }
    }
    }
    到這里,程序?qū)懲炅耍瑢ounter.java編譯為counter.class,同樣放在對(duì)應(yīng)的class目錄下,在根目錄下建立一個(gè)lyfcount.txt文件,文件內(nèi)容就一個(gè)數(shù)字0,直接在瀏覽器中敲入地址就可以看到計(jì)數(shù)器了,刷新瀏覽器會(huì)看到不斷變幻的數(shù)字。
    (如果運(yùn)行時(shí)候提示找不到文件,請(qǐng)將上面test.jsp中的readfile那一句注釋后運(yùn)行一次則lyfcount.txt文件自動(dòng)建立,然后就可以正常運(yùn)行。)
    為最佳答案評(píng)分?
    100% (1)
    不好 0% (0)
    (目前有 1 個(gè)人評(píng)價(jià))

    其 他 回 答共2條

    1樓

    要用到session,application變量。
    知識(shí)庫(kù)標(biāo)簽: |列兵

    我來(lái)回答這個(gè)問(wèn)題

    立即登陸回答獲取會(huì)員積分,提高用戶(hù)級(jí)別
    友情鏈接:
    Copyright © 商名網(wǎng) All Rights Reserved.