初学者问题,javabean计数器高分求解

guyanduzui 2003-03-16 05:22:59
出错:http://yu.40it.com/jsp/count2.jsp
文件:
counter.java
package counter;
import java.io.*;
public class counter extends Object
{
public String path = "";
public String doCount() throws FileNotFoundException
{
BufferedReader file; //BufferedReader对象,用于读取文件数据
String countFile = path;
//读取count.txt中的访问人数
file = new BufferedReader(new FileReader(countFile));
String readStr = "";
int writeStr = 1; //如果数据库为空则让它显示时变成1并写入
try
{
readStr = file.readLine();
}
catch(IOException e) {
System.out.println("读取数据错误.");
}
if (readStr == "")
{
readStr = "没有任何记录"; //访问人数加1
} else {
writeStr = Integer.parseInt(readStr) + 1;
}
//将新的访问人数写入count.txt
try
{
PrintWriter pw = new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
return readStr;
}
}
count2.jsp
<HTML>
<HEAD>
<meta http-equiv="Content_Type" content="text/html;charset=gb2312">
<META NAME="GENERATOR" CONTENT="Oracle JDeveloper">
<TITLE>计数器JavaBeans版本</TITLE>
</HEAD>
<BODY bgcolor="#ffffff">
<jsp:useBean id="counter" scope="request" class="counter.counter"/>
<%@ page contentType="text/html;charset=gb2312"%>
<%
counter.path = "count.txt";
String count = counter.doCount();
//读取访问人数并实现自动增加
%>
<p align="center">
<H1>计数器JavaBeans版</h1>
<H3>你是本网页的第<font color="ff0000" size="7">
<%=count%>
</font>名访客!</h3>
</BODY>
</HTML>
...全文
42 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnyshine 2003-09-14
  • 打赏
  • 举报
回复
hehe
zxhong 2003-03-27
  • 打赏
  • 举报
回复
to: icecloud(冰云)
习惯成自然,最好记住该id
以后不回答就是
林子大了,什么鸟都有!
mark
icecloud 2003-03-26
  • 打赏
  • 举报
回复
你怎么分配分的啊??!!

up的人比解决问题的人分高???!!!
freelarry 2003-03-26
  • 打赏
  • 举报
回复
up
erlia 2003-03-26
  • 打赏
  • 举报
回复
up
onekey 2003-03-25
  • 打赏
  • 举报
回复
按照icecloud(冰云) 的程序为什么每次会加2呢?
migrant1119 2003-03-23
  • 打赏
  • 举报
回复
String path=application.getRealPath("/");
这句不会有问题的。
其实我建议你用一个txt文件进行存取,这样方便,数据库浪费了。
用文件的时候必须要用realPath,而且在你的网站中的ftp必须设定权限。
icecloud 2003-03-23
  • 打赏
  • 举报
回复
还哪不行啊?
guyanduzui 2003-03-22
  • 打赏
  • 举报
回复
还是不行,大家帮我呵~~~~~~~~~~
handsome1234 2003-03-16
  • 打赏
  • 举报
回复
应该没有问题了吧?
zxhong 2003-03-16
  • 打赏
  • 举报
回复
counter.path = "count.txt";
//要有路径!
eg:c:\\count.txt

当然用相对路径也可以:
String path=application.getRealPath("/");//取根目录
icecloud 2003-03-16
  • 打赏
  • 举报
回复
你这个用法不太对啊
path应该修改一下

public class counter extends Object
{
private String path = "";
private String count;
public String doCount() throws FileNotFoundException
{
BufferedReader file; //BufferedReader对象,用于读取文件数据
String countFile = path;
//读取count.txt中的访问人数
file = new BufferedReader(new FileReader(countFile));
String readStr = "";
int writeStr = 1; //如果数据库为空则让它显示时变成1并写入
try
{
readStr = file.readLine();
}
catch(IOException e) {
System.out.println("读取数据错误.");
}
if (readStr == "")
{
readStr = "没有任何记录"; //访问人数加1
} else {
writeStr = Integer.parseInt(readStr) + 1;
}
//将新的访问人数写入count.txt
try
{
PrintWriter pw = new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
this.count = readStr;
}
public String getPath(){
return this.path;
}
public void setPath(String p ){
this.path=p;
this.count = this.doCount();
}
public String getCount(){
return this.count;
}
}

然后是调用的时候:
<jsp:useBean id="counter" scope="request" class="counter.counter"/>
<jsp:setProperty name="counter" property="path" value="/counter.txt" />
然后<jsp:getProperty name="counter" property="count" />

这才是标准的bean的写法 写多了你就知道这样写的方便之处了

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧