我爱电脑技术论坛's Archiver

炼域人生 发表于 2008-4-28 00:30

jsp计数器制作手册

计数器是一般网站必备的东东,别小看它了,每当站长看着小小计数器上的数字飞速增长的时候,感觉实在是好极了。以前我们用cgi、asp来写计数器,这方面的文章很多了,在这里,我们将会采用目前比较流行的jsp技术演示如何做一个计数器。 4G9On s n0lr

VfJE0S1{   其中我们用到了两个文件,test.jsp文件用于在浏览器中运行,counter.java是后台的一个小java bean程序,用来读计数器的值和写入计数器的值。而对于计数器的保存,我们采用了一个文本文件lyfcount.txt。
&x t?%L ~(M 7c6EXt/h,n ?
下面是详细的程序代码(test.jsp放到web目录下,counter.java放到class目录): 7j~,eWL(Sq{
//test.jsp文件
+a KK7fUR,L <%@ page contentType="text/html;charset=gb2312"%>
)e1D?.O*g/G <HTML> '] g,wl5Jd F&O
<HEAD>
EF C/A| ?7u0@(Q <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
o/]Y_ E3N[ <META NAME="GENERATOR" CONTENT="Oracle JDeveloper"> I[P\&SS5Vy
<TITLE> 5Im2INwx
计数器演示程序 {ZaZ(~Ma/n
</TITLE>
!|-yz k*CH_ </HEAD> -C)XO {5G%`0_p6lN w
<BODY>
d-LbV X!` U <!--创建并调用bean(counter)-->
1x.KU I^ E2}8U` <jsp:useBean id="counter" class="counter" scope="request">
9Y)h0W5U m </jsp:useBean> )s1V DCf9L]ou
<%
{],[fa'u(dkz bo //调用counter对象的ReadFile方法来读取文件lyfcount.txt中的计数 ,A}Xm#i&@/om8C
String cont=counter.ReadFile("/lyfcount.txt"); cy iu6NQ
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
6asd"b$`z3Ny"`*P counter.WriteFile("/lyfcount.txt",cont);%>
1?4fs)nwlr$i@ 您是第<font color="red"><%=cont%></font>位访问者
(G}R,i+z;c%nKHD7u </BODY>
R8M"l~z7e Ex-cT </HTML> MK K,cm
7Cc:q,wG2_A+|
//counter.java 读写文件的一个bean
A~:z!Q{,N'X+M4~5C import java.io.*; bs5N_:|
V i9ll3G9Q`M @+}.}
public class counter extends Object {
MB6w;M/t0v private String currentRecord = null;//保存文本的变量 IJW1PP0t$y9t
private BufferedReader file; //BufferedReader对象,用于读取文件数据 D&U6YDh ^;n6|T
private String path;//文件完整路径名
sGr)X)l$S public counter() { WSj!iK/J5G
} 'Em[Y2mh
//ReadFile方法用来读取文件filePath中的数据,并返回这个数据 ok4{kvBu(`
public String ReadFile(String filePath) throws FileNotFoundException vS'AH0I^
{ /V1q$wZ P6P$XPK2\
path = filePath;
)_9}"y-aY"|G7ZV+D //创建新的BufferedReader对象
8o7V5IPC4pBe\-g)L file = new BufferedReader(new FileReader(path)); 3s sJ&J\lR
String returnStr =null; gqj"J7yauI
try
Q_R3@%ld { fV X Z]*r3^+kZ
//读取一行数据并保存到currentRecord变量中
7}!jA!nul currentRecord = file.readLine();
$k1FpZH } zM3w8AX&@
catch (IOException e)
/L9_:sem3f J3]_(E {//错误处理 X4MV%p a
System.out.println("读取数据错误."); :BKt9K#wW
}
:f @YKd*y|"h if (currentRecord == null)
.g2Zr#Y.V;@ //如果文件为空 }U4zc:yA7F)dj
returnStr = "没有任何记录"; %k _}p*? od2pY
else
OOo!N`xr,x {//文件不为空
3CmQD%g@ returnStr =currentRecord; (wNI&i7t
}
ELon3|~ //返回读取文件的数据
C;S(U"X@.^u return returnStr;
oVY.k}1e6_!P? Qd4xz }
7` `I,yeU{ //ReadFile方法用来将数据counter+1后写入到文本文件filePath中
+hU N Ieh'? //以实现计数增长的功能 +n1I-Tt%j!{.n6kU
public void WriteFile(String filePath,String counter) throws FileNotFoundException
_3~Rk*AH$p R { "Y+A J"{7e"I
path = filePath; ,g5my7_S)IM`
//将counter转换为int类型并加一 /vg#L7m*y
int Writestr = Integer.parseInt(counter)+1; 8x ^Ws(c_"W
try { hi8VW$c E
//创建PrintWriter对象,用于写入数据到文件中
xE?^p n PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
3wc4l&|c0I //用文本格式打印整数Writestr &xOAk z
pw.println(Writestr); kv6FkO,t3Wk?
//清除PrintWriter对象
v-S.OOj.E'w$Kh8]3b"s pw.close(); .BJc-}Au
} catch(IOException e) { yD'b*[&A%Q(j;K
//错误处理
8I/^2b4z)t\ System.out.println("写入文件错误"+e.getMessage()); Un;b+uU
}
!zp?sEJFkU } cN q8N{q._ h~1r)i

;D{a;A}9p }
e_9?4U%i"v
o ]Rvfz tg7X   到这里,程序写完了,将counter.java编译为counter.class,同样放在对应的class目录下,在根目录下建立一个lyfcount.txt文件,文件内容就一个数字0,直接在浏览器中敲入地址就可以看到计数器了,刷新浏览器会看到不断变幻的数字。
4|t J7E7RRh l9PG9@ HM d
  (如果运行时候提示找不到文件,请将上面test.jsp中的readfile那一句注释后运行一次则lyfcount.txt文件自动建立,然后就可以正常运行。)

页: [1]
   

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.