做一个带有beans的文字计数器 访问计数器程序:counter.Java
package counter; import java.io.*; public class counter { public String path=''; public String doCount() throws FileNotFoundException { BufferedReader file; String countFile=path; file=new BufferedReader(new FileReader(countFile)); String readStr=''; int writeStr=1; try { readStr=file.readLine(); } catch(IOException e) { System.out.println('读取数据错误.'); }
if(readStr=='') readStr='没有任何记录'; else { writeStr=Integer.parseInt(readStr)+1; }
try { PrintWriter pw=new PrintWriter(new FileOutputStream(countFile)); pw.println(writeStr); pw.close(); } catch(IOException e) { System.out.println(e.getMessage()); } return readStr; } }
counter.JSP代码如下: <HTML> <HEAD> <TITLE> counter </TITLE> </HEAD> <BODY> <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'> <H3 align='center'><font color='#993300' size='5'>您好!您是本系统的第 <font color='ff0000'><%=count%> </font> 名访问者!</font></H3> </body> </html>
|