Java Bean实现网页来访计数器
2009-10-25 20:53:50 来源:WEB开发网1.jsp页:
<%--
Document : index
Created on : 2009-10-10, 14:47:46
Author : lucifer
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="count" class="MyBean.Counter"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="#FFFFCC">
<center>
<font size="+5" face="楷体">Welcome!!</font><br>
You're the
<%
String url = "C:\\Users\\lucifer\\Desktop\\count.txt"; //改成自己的text文件,用来计数
count.setPath(url);
count.setCount();
out.PRintln(count.getCount());
%>
Visitor!!!
</center>
</body>
</html>
2.Bean:
package MyBean;
import java.io.*;
/**
*
* @author lucifer
*/
public class Counter {
private String url;
private String count;
public void setPath(String url){
this.url = url;
}
public String getCount(){
try{
BufferedReader br;
br = new BufferedReader(new FileReader(url));
count = br.readLine();
br.close();
return count;
}
catch(IOException e){
e.printStackTrace();
return "No Record!";
}
}
public void setCount(){
try{
BufferedReader br = new BufferedReader(new FileReader(url));
count = new String(br.readLine());
int num;
if(count == null)
num = 1;
else
num = Integer.parseInt(count) + 1;
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(url));
pw.println(num);
pw.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
更多精彩
赞助商链接