posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          單用戶,無刷新,計數(shù)器

          Posted on 2008-09-10 14:54 semovy 閱讀(244) 評論(0)  編輯  收藏 所屬分類: JSP
          <%@ page contentType="text ml;charset=gb2312"%> 
          <%@ page language="java" import="java.io.*"%> 

          <html> 
          <head> 
          <meta http-equiv="content-type" content="text ml; charset=gb2312"> 
          <title>計數(shù)器</title> 
          </head> 

          <%! 
          //同步更新計數(shù)器 
          synchronized void counter(){ 
          ServletContext application=getServletContext(); //構(gòu)造application對象(可選) 
          String szpath=application.getRealPath("/"); //得到當(dāng)前路徑 
          szpath=szpath+"hits.txt"; //計數(shù)器文檔 0-9999999999999... 
          String szrecord=""; //記數(shù) string 
          int nrecord=0; //記數(shù) int 

          try{ 
          BufferedReader file=new BufferedReader(new FileReader(szpath)); 
          szrecord=file.readLine(); //讀取計數(shù)器文檔 
          catch(IOException e){ 
          e.printStackTrace(System.err); 
          if(szrecord==null) szrecord="0"; //假如計數(shù)器文檔為空 

          nrecord=java.lang.Integer.parseInt(szrecord)+1; //計數(shù)器+1 
          try{ 
          File f=new File(szpath); 
          PrintWriter pw=new PrintWriter(new FileWriter(f)); 
          pw.print(nrecord); //寫文檔 
          pw.close(); 
          catch(IOException e){ 
          System.out.println(e); 
          %> 

          <% 
          //顯示計數(shù)器 
          if(session.isNew()){ //假如是新用戶 
          counter(); 
          String path=application.getRealPath("/"); 
          String szpath=path+"hits.txt"; 
          String szrecord=""; 
          BufferedReader file=new BufferedReader(new FileReader(szpath)); 
          try{ 
          szrecord=file.readLine(); 
          catch(IOException e){ 
          System.out.println("文檔未找到!"); 
          //顯示7位數(shù)字gif圖像 
          String szout="<body topmargin=0 leftmargin=0>"; 
          int i=0; 
          int k=7-szrecord.length(); //"0"的個數(shù) 
          for (i=0;i<k-1;i++){ //顯示"0" 
          szout=szout+"<img src=images/0.gif>"; 
          for (i=0;i<szrecord.length();i++){ //顯示非"0" 
          szout=szout+"<img src=images/"+ szrecord.charAt(i) +".gif>"; 

          主站蜘蛛池模板: 华蓥市| 保康县| 宜章县| 扶风县| 潮安县| 河间市| 双牌县| 翁牛特旗| 景宁| 蚌埠市| 九龙县| 九寨沟县| 锡林浩特市| 巴青县| 新津县| 兰考县| 东兰县| 余江县| 顺昌县| 岢岚县| 兰州市| 井冈山市| 大同县| 临桂县| 永安市| 浑源县| 大冶市| 邢台县| 拉孜县| 宝山区| 东莞市| 峨山| 衢州市| 宁远县| 陕西省| 家居| 九龙城区| 广东省| 浦北县| 嘉祥县| 金乡县|