如何用jsp+txt寫網站計數器????在線等!!

davidlau 2003-08-22 11:53:13
如何用jsp+txt寫網站計數器????請給出源碼,最好不用.class,謝謝!!!
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sensory 2003-08-22
  • 打赏
  • 举报
回复
刚搞过!www.java-cn.com 上有一个计数器的例子!上的注意修改一下
就是那个自动生成的那个地方 自己先添加一个声明即可!
loverface 2003-08-22
  • 打赏
  • 举报
回复
javabean:
public String count() {
String ret="";
File file=new File("count.txt");
if(!file.exists())
{
System.out.println("Error:file is not exists!");
}
try {
BufferedReader fread=new BufferedReader(new FileReader(file));
String fcon=fread.readLine();
fread.close();
long cou=Long.parseLong(fcon);
cou=cou + 1;
DecimalFormat df= new DecimalFormat("#00000000");
ret=df.format(cou);
PrintWriter fout=new PrintWriter(new FileWriter(file));
fout.print(ret);
fout.close();
}
catch(IOException er) {
System.out.println("文件读写错误:" + er.getMessage());
}
return ret;
}

jsp:
<p align="center">您是第
<%String count=javabean.count();
for(int i=0;i<test.length();i++){%>
<img src="images/<%=count.charAt(i) %>.gif" width="10" height="23">
<%}%>
位访客</p>
seaman0916 2003-08-22
  • 打赏
  • 举报
回复
在www.java-cn.com 上有一个计数器的例子!
真好是JSP+记事本 写的!和你的要求相吻合!
你去看看吧!
wellsoon 2003-08-22
  • 打赏
  • 举报
回复
jsp的实现,如果计数器文件不存在,将自动在jsp文件所在目录下自动创建:

counter.jsp
-----------------------------------------------------
<%@ page contentType="text/html; charset=GBK" %>
<%
response.setHeader("Pragma","No-cache");//HTTP 1.1
response.setHeader("Cache-Control","no-cache");//HTTP 1.0
response.setHeader("Expires","0");//防止被proxy
request.setCharacterEncoding("GB2312");//设置编码
%>
<html>
<head>
<title>
计数器
</title>
</head>
<body bgcolor="#ffffff">
<h1>
一个简单的计数器
</h1>
<%
try {
java.io.FileReader fr=new java.io.FileReader(request.getRealPath("/")+"\\counter.txt");
java.io.BufferedReader br = new java.io.BufferedReader(fr);
String countno = br.readLine();
if (countno == null) {
java.io.FileWriter fw=new java.io.FileWriter(request.getRealPath("/")+"\\counter.txt");
java.io.PrintWriter pw=new java.io.PrintWriter(fw);
pw.print("1");
pw.close();
fw.close();
}
else{
out.println("本页面是第"+countno+"次被访问!!");
java.io.FileWriter fw=new java.io.FileWriter(request.getRealPath("/")+"\\counter.txt");
java.io.PrintWriter pw=new java.io.PrintWriter(fw);
pw.print(String.valueOf(Integer.parseInt(countno)+1) );
pw.close();
fw.close();
}}
catch(Exception e){
java.io.FileWriter fw=new java.io.FileWriter(request.getRealPath("/")+"\\counter.txt");
java.io.PrintWriter pw=new java.io.PrintWriter(fw);
pw.print("1");
out.println("本页面是第"+1+"次被访问!!");
pw.close();
fw.close();
}
%>
</body>
</html>


dext 2003-08-22
  • 打赏
  • 举报
回复
只要你的变量声明用<%! %>而不是<% %> 就可以做一个计数器
你的每一个页面都可以引用它,
如:(每一个页面都是这样)
<%! int i; %>
<% out.print(++i); %>

如果你的服务器要重启的话,记得把它放到数据库或文本文件里就可以了!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧