加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

jsp加入文本计数器

(2008-11-07 10:39:20)
标签:

编程

java

it

分类: 计算机

<%@ page contentType="text/html;charset=gb2312" %>
<!--jsp计数器-->
<html>
<head>
<title>jsp计数器</title>
</head>
<body>
<%@ page import="java.io.*" %>
<%
String currentRecord = null;//保存文本的变量
BufferedReader file; //BufferedReader对象,用于读取文件数据
String nameOfTextFile = "c:\\count.txt";

//如果是相对路径request.getRealPath("/")+"count.txt"; 

//读取
file = new BufferedReader(new FileReader(nameOfTextFile));
String readStr =null;
int writeStr =0; //如果计数文本中的计数值为空则让它显示时变成1并写入
try
{

    readStr = file.readLine();

 }
catch (IOException e)
{ System.out.println("读取数据错误."); }
if (readStr == null) readStr = "没有任何记录";

//判断cookie,第一次登陆时加1,刷新时不累计计数

else
{ writeStr = Integer.parseInt(readStr)+1;}

//写入时控制因为刷新引起的重复计数

try {
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(writeStr);
pw.close();}
catch(IOException e) {
out.println(e.getMessage());}

%>
<p align="center">您是CNJSP的第<b><font color="red"><%=writeStr%></font></b>位客人。</p>
</body>
</html>

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有