当前位置: 移动技术网 > IT编程>开发语言>Jsp > JSP计数器

JSP计数器

2019年04月19日  | 移动技术网IT编程  | 我要评论

    许多地方都会用到的(sun企业级应用的首选)计数器,希望对大家有帮助,做为收藏之用吧。

<%@ 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);

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网