您的位置:首页 > Web前端 > JavaScript

JSP网页简单计数器 代码片段分享

2012-01-06 01:08 337 查看
闲的蛋疼,上一阵子写过一个简单网页计数代码共享
这些也都是一些基础,主要涉及的是 IO 操作, 废话不说, 代码是最好的导师, 哥哥我写的代码 如果 有人看不明白的话, 那你丫的赶紧 改行算了,不要在 IT 行业 混了, 耽误你的前途啊 。


呃......又扯远了,切入正题 , 列出的代码如下:(开不懂得的地方,需要看注释的啦,不解释,地球人都知道)

注意参考可以
尊重一下原创
一些
劳动成果。 不解释,地球人都知道。^-^

1. 首先先 写一个 计数bean (命名为:Counter.java)

package com.songyanjun.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
* 描述: TODO  计数BEAN
*
* @类名称: Counter
* @作者: 宋延军
* @邮箱: songyanjun_stars@126.com
* @日期: Dec 25, 2011 11:10:23 PM
*/
public class Counter extends Object {

private String currentRecord = null;	// 保存文本的变量
private BufferedReader file;			// BufferedReader对象,用于读取文件数据
private String path;					// 文件完整路径名

/*
* 空构造方法
*/
public Counter()  { }

/**
* 描述: TODO ReadFile方法用来读取文件filePath中的数据,并返回这个数据
*
* @标题: ReadFile
* @param filePath
* @return
* @throws FileNotFoundException
* @返回类型: String
*/
public String ReadFile(String filePath) throws FileNotFoundException {
path = filePath;

file = new BufferedReader(new FileReader(path));   // 创建新的BufferedReader对象
String returnStr = null;
try  {
currentRecord = file.readLine();				// 读取一行数据并保存到currentRecord变量中
}
catch (IOException e) {
System.out.println("读取数据错误.");				// 错误处理
}

if (currentRecord == null) {
returnStr = "没有任何记录";						// 如果文件为空
} else {
returnStr = currentRecord;						// 文件不为空
}

return returnStr;									// 返回读取文件的数据
}

/**
* 描述: TODO ReadFile方法用来将数据counter+1后写入到文本文件filePath中 以实现计数增长的功能
*
* @标题: WriteFile
* @param filePath
* @param counter
* @throws FileNotFoundException
* @返回类型: void
*/
public void WriteFile(String filePath, String counter) throws FileNotFoundException {

path = filePath;
int Writestr = Integer.parseInt(counter) + 1;							// 将counter转换为int类型并加 1
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));	// 创建PrintWriter对象,用于写入数据到文件中
pw.println(Writestr);												// 用文本格式打印整数Writestr
pw.close();															// 清除PrintWriter对象
}
catch (IOException e) {			System.out.println("写入文件错误" + e.getMessage());					// 错误处理
}
}

}

2. 创建一个调用页面JSP页面
命名
为:WebpageSimpleCounter.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>
<%@page import="com.songyanjun.utils.Counter"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>JSP网页简单计数器 代码片段</title>
<meta http-equiv="X-Ua-Compatible" content="IE=7" />
<meta http-equiv="keywords" content="军军工作室,宋延军工作室,宋延军">
<meta http-equiv="description" content="军军工作室,打造个性自我主页OH MAN!!!">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="Copyright" content="军军工作室 http://www.songyanjun.net/" />
<link rel="shortcut icon" href="favicon.ico" />
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
//创建 计数类对象
Counter counter = new Counter();

//调用counter对象的ReadFile方法来读取文件jishuCount.txt中的计数
String cont=counter.ReadFile("d:\\jishuCount.txt");

//调用counter对象的ReadFile方法来将计数器加一后写入到文件jishuCount.txt中
counter.WriteFile("d:\\jishuCount.txt",cont);

%>

您是第<font color="red"><%=cont%></font>位访问者

</body>
</html>

之后直接发布Tomcat 里面去吧,
打开浏览器 访问, 你就能看到
计数器随着每次刷新 累加啦。






o(︶︿︶)o 唉, 真实闲的 蛋疼啊。。
代码摆在上面了。。
需要观摩自己 看去。。。

Over !!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: