⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 60.html

📁 关于jsp的一些好文章 主要介绍一些关于JSP的应用技巧方面的东西
💻 HTML
字号:

<STYLE type=text/css>
<!--
body,td { font-size:9pt;}
hr { color: #000000; height: 1px}
-->
</STYLE>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>精选文章 >> 文件操作 >> jsp计数器制作手册</title>
</head>
<body >

<p><IMG SRC="../image/jsp001_middle_logo.gif" WIDTH="180" HEIGHT="60" BORDER=0 ALT=""></p>

<table width=100% bgcolor="#cccccc" align=center cellpadding="2" cellspacing="0" border=1 bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr bgcolor="#EFF8FF"><td>
<a href=http://www.jsp001.com/list_thread.php?int_attribute=2>精选文章</a>
>> <a href=http://www.jsp001.com/list_thread.php?forumid=16&int_attribute=2>文件操作</a>
>> jsp计数器制作手册 [<a href=http://www.jsp001.com/forum/showthread.php?goto=newpost&threadid=60>查看别人的评论</a>]<br>

<hr><p>由 webmaster 发布于: 2001-01-20 16:33</p><p> </p><p>作者:刘玉锋 来源:http://aspfans.yeah.net<br><br>计数器是一般网站必备的东东,别小看它了,每当站长看着小小计数器上的数字飞速增长的时候,感觉实在是好极了。以前我们用cgi、asp来写计数器,这方面的文章很多了,在这里,我们将会采用目前比较流行的jsp技术演示如何做一个计数器。<br>其中我们用到了两个文件,test.jsp文件用于在浏览器中运行,counter.java是后台的一个小java bean程序,用来读计数器的值和写入计数器的值。而对于计数器的保存,我们采用了一个文本文件lyfcount.txt。<br><br><br>下面是详细的程序代码(test.jsp放到web目录下,counter.java放到class目录):<br><br><br>//test.jsp文件<br>&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;<br>&lt;TITLE&gt;计数器演示程序&lt;/TITLE&gt;<br>&lt;/HEAD&gt;<br>&lt;BODY&gt;<br>&lt;!--创建并调用bean(counter)--&gt;<br>&lt;jsp:useBean id="counter" class="counter" scope="request"&gt;<br>&lt;/jsp:useBean&gt;<br>&lt;%<br>//调用counter对象的ReadFile方法来读取文件lyfcount.txt中的计数<br>String cont=counter.ReadFile("/lyfcount.txt");<br>//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中<br>counter.WriteFile("/lyfcount.txt",cont);%&gt;<br>您是第&lt;font color="red"&gt;&lt;%=cont%&gt;&lt;/font&gt;位访问者<br>&lt;/BODY&gt;<br>&lt;/HTML&gt;<br><br>//counter.java 读写文件的一个bean<br>import java.io.*;<br><br>public class counter extends Object {<br>private String currentRecord = null;//保存文本的变量<br>private BufferedReader file; //BufferedReader对象,用于读取文件数据<br>private String path;//文件完整路径名<br>public counter() {<br>}<br>//ReadFile方法用来读取文件filePath中的数据,并返回这个数据<br>public String ReadFile(String filePath) throws FileNotFoundException<br>{<br>path = filePath;<br>//创建新的BufferedReader对象<br>file = new BufferedReader(new FileReader(path));<br>String returnStr =null;<br>try<br>{<br>//读取一行数据并保存到currentRecord变量中<br>currentRecord = file.readLine();<br>}<br>catch (IOException e)<br>{//错误处理<br>System.out.println("读取数据错误.");<br>}<br>if (currentRecord == null)<br>//如果文件为空<br>returnStr = "没有任何记录";<br>else<br>{//文件不为空<br>returnStr =currentRecord;<br>}<br>//返回读取文件的数据<br>return returnStr;<br>}<br>//ReadFile方法用来将数据counter+1后写入到文本文件filePath中<br>//以实现计数增长的功能<br>public void WriteFile(String filePath,String counter) throws <br><br>FileNotFoundException<br>{<br>path = filePath;<br>//将counter转换为int类型并加一<br>int Writestr = Integer.parseInt(counter)+1;<br>try {<br>//创建PrintWriter对象,用于写入数据到文件中<br>PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));<br>//用文本格式打印整数Writestr<br>pw.println(Writestr);<br>//清除PrintWriter对象<br>pw.close();<br>} catch(IOException e) {<br>//错误处理<br>System.out.println("写入文件错误"+e.getMessage());<br>}<br>}<br><br>}<br><br>到这里,程序写完了,将counter.java编译为counter.class,同样放在对应的<br><br>class目录下,在根目录下建立一个lyfcount.txt文件,文件内容就一个数字0,直接在<br><br>浏览器中敲入地址就可以看到计数器了,刷新浏览器会看到不断变幻的数字。<br>(如果运行时候提示找不到文件,请将上面test.jsp中的readfile那一句注释后运行<br><br>一次则lyfcount.txt文件自动建立,然后就可以正常运行。)<br><br><br>上面的程序完整源代码在我的网站http://aspfans.yeah.net上可以下载。 <br><br></p></td>
  </tr>
</table>

<p>
<CENTER><a href="http://www.jsp001.com/forum/newreply.php?action=newreply&threadid=60">点这里对该文章发表评论</a></CENTER>
<p>该文章总得分是 <font color=red>0</font> 分,你认为它对你有帮助吗?
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=60&intVote=4","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>非常多</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=60&intVote=2","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>有一些</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=60&intVote=1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>无帮助</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=60&intVote=-1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>是灌水</a>](<font color=red>0</font>) </p>
<script language="javascript" src="http://www.jsp001.com/include/read_thread_script.php?threadid=60"></script>
<p><CENTER>
Copyright &copy; 2001 - 2009 JSP001.com . All Rights Reserved <P>

<IMG SRC="../image/jsp001_small_logo.gif" WIDTH="85" HEIGHT="30" BORDER=0 ALT="">
</CENTER></p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -