zoukankan      html  css  js  c++  java
  • 利用java实现简单图片的计数器

    利用java实现简单图片的计数器,运行图:

    想学习更多关于java的知识,可以点击《Java EE软件工程师》进行学习。

    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page language="java" import="java.io.*"%> 
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>计数器</title>
    </head>

    <%!
    //同步更新计数器

     

    synchronized void counter(){ 

           ServletContext application=getServletContext();   //构造application对象(可选)

            String szPath=application.getRealPath("/");       //得到当前路径

                    szPath=szPath+"hits.txt";                //计数器文件 0-9999999999999...

            String szRecord="";                               //记数 String

            int nRecord=0;                             //记数 int

            try{ 

               BufferedReader file=new BufferedReader(new FileReader(szPath));

                szRecord=file.readLine();                     //读取计数器文件

            }

            catch(Exception e){ 

               System.out.println(e); 

           }

      if(szRecord==null){

       szRecord="0";                                         //如果计数器文件为空

            } 

           nRecord=java.lang.Integer.parseInt(szRecord)+1;    //计数器+1

      try{  

              File f=new File(szPath);

                PrintWriter pw=new PrintWriter(new FileWriter(f));

                pw.print(nRecord); //写文件。这种频繁写文件保存记数的方法,不可取。 

               pw.close();

            } 

           catch(Exception e){  

              System.out.println(e); 

           } 

       }  

    %>

    <% 

    //显示计数器
        if(session.isNew()){                               //如果是新会话
            counter();
        }    
        String Path=application.getRealPath("/");
        // out.println(Path);
        String szPath=Path+"hits.txt";
        String szRecord="";
        BufferedReader file=new BufferedReader(new FileReader(szPath));
        try{
            szRecord=file.readLine();
            if(szRecord==null){
               szRecord="0";  
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
     //显示7位数字gif图像
        String szOut="<body topmargin='0' leftmargin='0'>当前访问量:";
        int i=0;
        int k=7-szRecord.length();                          //"0"的个数
        for (i=0;i<k;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'>";
        }
        szOut=szOut+"</body>";
        out.println(szOut);     
    %>

    </html>

    想了解更多java学习课程,可以关注e良师益友

  • 相关阅读:
    Codeforces 1265A Beautiful String
    1039 Course List for Student (25)
    1038 Recover the Smallest Number (30)
    1037 Magic Coupon (25)
    1024 Palindromic Number (25)
    1051 Pop Sequence (25)
    1019 General Palindromic Number (20)
    1031 Hello World for U (20)
    1012 The Best Rank (25)
    1011 World Cup Betting (20)
  • 原文地址:https://www.cnblogs.com/xiaoxiao1/p/4090196.html
Copyright © 2011-2022 走看看