大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> JSP计数器的制作

JSP计数器的制作

时间: 2021-07-31 作者:daque

计数器是普遍网站必备的东东,别忽视它了,每当站长看着小小计数器上的数字飞快延长的功夫,发觉简直是好极了。往日咱们用cgi、asp来写计数器,这上面的作品很多了,在这边,咱们将会沿用暂时比拟时髦的jsp本领演练怎样做一个计数器。个中咱们用到了两个文献,test.jsp文献用来在欣赏器中运转,counter.java是后盾的一个小java bean步调,用来读计数器的值和写入计数器的值。而对于计数器的生存,咱们沿用了一个文本文献lyfcount.txt。底下是精细的步调代码(test.jsp放到web目次下,counter.java放到class目次)://test.jsp文献<%@ page contenttype="text/html;charset=utf8"%><html><head><meta http-equiv="content-type" content="text/html; charset=utf8"><title>计数器演练步调</title></head><body><!--创造并挪用bean(counter)--><jsp:usebean id="counter" class="counter" scope="request"></jsp:usebean><%//挪用counter东西的readfile本领来读取文献lyfcount.txt中的计数string cont=counter.readfile("/lyfcount.txt");//挪用counter东西的readfile本领来将计数器加一后写入到文献lyfcount.txt中counter.writefile("/lyfcount.txt",cont);%>您是第<font color="red"><%=cont%></font>位考察者</body></html>//counter.java 读写文献的一个beanimport java.io.*;public class counter extends object {private string currentrecord = null;//生存文本的变量private bufferedreader file; //bufferedreader东西,用来读取文献数据private string path;//文献完备路途名public counter() {}//readfile本领用来读取文献filepath中的数据,并归来这个数据public string readfile(string filepath) throws filenotfoundexception{path = filepath;//创造新的bufferedreader东西file = new bufferedreader(new filereader(path));string returnstr =null;try{//读取一条龙数据并生存到currentrecord变量中currentrecord = file.readline();}catch (ioexception e){//缺点处置system.out.println("读取数据缺点.");}if (currentrecord == null)//即使文献为空returnstr = "没有任何记载";else{//文献不为空returnstr =currentrecord;}//归来读取文献的数据return returnstr;}//readfile本领用来将数据counter+1后写入到文本文献filepath中//以实行计数延长的功效public void writefile(string filepath,string counter) throws filenotfoundexception{path = filepath;//将counter变换为int典型并加一int writestr = integer.parseint(counter)+1;try {//创造printwriter东西,用来写入数据到文献中printwriter pw = new printwriter(new fileoutputstream(filepath));//用文本方法打字与印刷平头writestrpw.println(writestr);//废除printwriter东西pw.close();} catch(ioexception e) {//缺点处置system.out.println("写入文献缺点"+e.getmessage());}}}到这边,步调写结束,将counter.java编写翻译为counter.class,同样放在对应的class目次下,在根目次下创造一个lyfcount.txt文献,文献实质就一个数字0,径直在欣赏器中敲入地方就不妨看到计数器了,革新欣赏器会看到连接变换的数字。(即使运转功夫提醒找不到文献,请将上头test.jsp中的readfile那一句解释后运转一次则lyfcount.txt文献机动创造,而后就不妨平常运转。)

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.