网站计数器
各位老兄快快帮我,网站计数器如何用jsp实现???? 问题点数:0、回复次数:6Top
1 楼icecloud(冰云)回复于 2003-09-01 19:08:47 得分 0
计数写到文件里面或者到数据库Top
2 楼fujiguncn(蚂蚁劳模)回复于 2003-09-01 19:18:17 得分 0
在页面上放一个计数器很难看的Top
3 楼ghostxinghe(娃哈哈)回复于 2003-09-01 19:18:41 得分 0
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%
Integer count = null;
synchronized(application)
{
count = (Integer)application.getAttribute("basic.counter");
if(count == null)
count = new Integer(0);
count = new Integer(count.intValue()+1);
application.setAttribute("basic.counter",count);
}
%>
<html>
<head>
<title>简单计数器</title>
</head>
<body>
<center>
<font size=10 color=blue>简单计数器</font>
<br>
<hr>
<br>
<font>你好,你是第<%= count %>客人</font>
</center>
</body>
</html>
Top
4 楼pleonheart(只睡六小时)回复于 2003-09-01 20:10:03 得分 0
执行一次页面+1,存文件或数据库Top
5 楼SlzAgain(学网络知识,黑日本网站)回复于 2003-09-01 20:28:55 得分 0
<%
request.getSession(true);
String driver= "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/fooldockdb?useUnicode=true&characterEncoding=gb2312";
String userID= "root";
String passwd= "xiaobao";
int OldCNT;
int UserCNT;
int Index;
String UserIndex;
String OldIndex;
try{
Class.forName(driver);
Connection con = DriverManager.getConnection(url,userID,passwd);
Statement stmt = con.createStatement();
String query = "SELECT * FROM counter;";
ResultSet QRes = stmt.executeQuery(query);
while(QRes.next()){
OldCNT = QRes.getInt("times");
UserCNT = OldCNT + 1;
UserIndex= UserCNT+"";
OldIndex=OldCNT+"";
if(session.isNew()){
for(Index=0;Index<UserIndex.length();Index++){
out.println("<img src=counter/image/c"+UserIndex.charAt(Index)+".gif align=absmiddle>");
}
QRes.close();
query = "UPDATE counter SET times =" + UserCNT;
stmt.executeQuery(query);
}
else{
for(Index=0;Index<OldIndex.length();Index++){
out.println("<img src=counter/image/c"+OldIndex.charAt(Index)+".gif align=absmiddle>");
}
QRes.close();
}
con.close();
}
}
catch(SQLException e){
e.printStackTrace();
out.print("有错误");
}
%>
Top
6 楼storejyjs(独行的狼)回复于 2003-11-25 22:56:43 得分 0
<html>
<body>
<%@ page import="java.io.*"%>
<%
BufferedReader file;
String countFile="c:\\count.txt";
String readStr=null;
int writeStr=1;
try
{
file=new BufferedReader(new FileReader(countFile));
readStr=file.readLine();
}catch(IOException e)
{
System.out.println("read data error");
}
if(readStr==null) readStr="no data";
else
{
writeStr=Integer.parseInt(readStr)+1;
}
try{
PrintWriter pw;
pw=new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
} catch(IOException e)
{
System.out.println(e.getMessage());
}
%>
<h1> you are the </h1>
<h3> <%=readStr%></h3>
<h1> vistor to the page</h1>
</body>
</html>
Top



