<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>
计数器演示程序
</TITLE>
</HEAD>
<BODY>
<!--创建并调用bean(counter)-->
<jsp:useBean id="count" class="testcounter.counter" scope="request">
</jsp:useBean><%
//调用counter对象的ReadFile方法来读取文件lyfcount.txt中的计数
String cont=count.readFile("lyfcount.txt");
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
count.writeFile("lyfcount.txt",cont);%>
您是第<font color="red"><%=cont%></font>位访问者
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>
计数器演示程序
</TITLE>
</HEAD>
<BODY>
<!--创建并调用bean(counter)-->
<jsp:useBean id="count" class="testcounter.counter" scope="request">
</jsp:useBean><%
//调用counter对象的ReadFile方法来读取文件lyfcount.txt中的计数
String cont=count.readFile("lyfcount.txt");
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
count.writeFile("lyfcount.txt",cont);%>
您是第<font color="red"><%=cont%></font>位访问者
</BODY>
</HTML>
count.writeFile("lyfcount.txt",cont);%
提示错误就是找不到文件!
String cont=count.readFile("lyfcount.txt"); 改成
String cont=count.readFile("a/lyfcount.txt");
编辑D:\Webservers\Apache Tomcat 4.0\conf\server.xml
找到有Context的地方,插入:
<!-- Add by Fuchj 2003-02-26 for test one demo including workflow -->
<Context path="/dreambike" docBase="dreambike" debug="0" privileged="true" />
其中path是http访问的路径如上例为:http://localhost:8080/dreambike
docBase是程序存放的路径
重启动tomcat
我的javabean如下:
package database;import java.sql.*;public class OPDB {
private String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private String sDBConn="jdbc:odbc:bbs";
private Connection conn=null;
private ResultSet rs=null;
private Statement stmt=null; public OPDB() {
try{
Class.forName(sDBDriver);
}
catch(Exception e){
e.printStackTrace();
}
} public ResultSet select(String sql){
try{
conn=DriverManager.getConnection(sDBConn, "system", "manager");
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException e){
e.printStackTrace();
}
return rs;
} public void update(String sql){
try{
conn=DriverManager.getConnection(sDBConn, "sa", "");
stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException e){
e.printStackTrace();
}
} public void Close(){
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
package testcounter;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: riozn</p>
* @zhouzm
* @version 1.0
*/import java.io.*;public class counter {
private String currentRecord=null;
private BufferedReader file=null;
private String path=null; public counter() { } public String readFile(String fileName){
path=fileName;
String returnStr=null;
try{
file=new BufferedReader(new FileReader(path));
}
catch(FileNotFoundException e1){
System.out.println("文件没有找到!");
return null;
}
try{
currentRecord=file.readLine();
}
catch(IOException e){
System.out.println("读取数据错误.");
return null;
}
if (currentRecord==null)
returnStr="100000";
else{
returnStr=currentRecord;
}
return returnStr;
} public void writeFile(String fileName, String counter) throws FileNotFoundException{
path=fileName;
int writeStr=Integer.parseInt(counter)+1;
try{
PrintWriter pw=new PrintWriter(new FileOutputStream(path));
pw.println(writeStr);
pw.close();
}
catch(IOException e){
System.out.println("写入文件错误"+e.getMessage());
}
}
}