JSP中实现网页访问统计的方法(4)
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class JSPCount {
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
在WebRoot目录下建jsp文件:count.jsp
[plain]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
相关新闻>>
- 发表评论
-
- 最新评论 更多>>