Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
JSP计数器

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    许多地方都会用到的JSP计数器,希望对大家有帮助,做为收藏之用吧。

<%@ 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(IOException e){
            e.printStackTrace(System.err);
        }
        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(IOException e){
            System.out.println(e);
        }
    } 
%>

<% 
//显示计数器
    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位数字gif图像
    String szOut="<body topmargin='0' leftmargin='0'>";
    int i=0;
    int k=7-szRecord.length();                                     //"0"的个数
    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'>";
    }
    szOut=szOut+"</body>";
    out.println(szOut);    
%>
</html>




相关文章

相关软件