为啥不能计数。能正常运行就是不能统计!
java文件:
/**
* 编写以下SessionCounter.java
* 并编译为SessiionCounter.class
* 然后放到你的网站的classpath的
* SessionCount(自己建立此目录)下面
*/
package SessionCount;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
public class SessionCounter implements HttpSessionListener {
private static int activeSessions = 0;
private static int totalVisit = 0;
private static int todayVisit = 0;
private static String countFilePath = "";
//private static String s = null;
public void sessionCreated(HttpSessionEvent se){
ServletContext context = se.getSession().getServletContext();
try
{
//countFilePath = context.getInitParameter("countFilePath");
countFilePath ="e:\\jspfile\\count.txt";
File f = new File(countFilePath);
if ( !f.exists() ) f.createNewFile();
FileInputStream fis = new FileInputStream(countFilePath);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
fis.close();
isr.close();
br.close();
FileOutputStream writeConut = new FileOutputStream(countFilePath);
OutputStreamWriter osw = new OutputStreamWriter(writeConut, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
try
{
Calendar calendar = null;
calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
int currentTime = calendar.get(Calendar.HOUR_OF_DAY) + calendar.get(Calendar.MINUTE) + calendar.get(Calendar.SECOND);
activeSessions++;
if ( s != null)
{
totalVisit = Integer.parseInt(s);
}//须写入一个文件中
totalVisit++;
bw.write(Integer.toString(totalVisit));
if ( currentTime == 0 ) todayVisit = 0; else todayVisit++;
}
catch (Exception e)
{
throw e;
}
finally
{
bw.close();
osw.close();
writeConut.close();
}
}
catch (Exception e)
{
context.log(e.toString());
}
}
public void sessionDestroyed(HttpSessionEvent se) {
if( activeSessions > 0)
activeSessions--;
}
public static int getActiveSessions() {
return activeSessions;
}
public static int getTotalVisit() {
return totalVisit;
}
public static int getTodayVisit() {
return todayVisit;
}
public void setCountFilePath(String value) {
countFilePath = value;
}
}
jsp文件
<%
/**
* <p>
* <p> Description:站点首页。jsp的主要功能是:查询出数据库种类的新闻标题加入hyperlink。供点击进入 <p>
* <p> <p>
* <p> Copyright: Copyright (c) 2003 <p>
* <p> Company: chuanyi <p>
* @author: Yangjg <p>
* @version: 1.0 2003-9-3 11:04 <p>
*/
%>
<%@ page contentType="text/html; charset=utf-8" import="java.util.*, java.lang.*, java.sql.*,java.io.*"%>
<%@ page import ="java.text.*, SessionCount.SessionCounter" errorPage="../showError/showError.jsp"%>
<%
response.setContentType("text/html; charset=UTF-8");
%>
<jsp:useBean id = "sessionC" class="SessionCount.SessionCounter" scope="page"/>
<%
sessionC.setCountFilePath("e:\\jspfile\\count.txt");
%>
<table width="136" height="57" border="0" align="right" cellpadding="0" cellspacing="3">
<tr>
<td width="130" height="18"><font color="#000000" size="2">在线人数: <%=sessionC.getActiveSessions()%> 人 </font></td>
</tr>
<tr>
<td height="19"><font color="#000000" size="2">今日访问: <%=sessionC.getTodayVisit()%> 人</font></td>
</tr>
<tr>
<td height="5"><font color="#000000" size="2">IP: <%=request.getRemoteAddr()%> </font></td>
</tr>
<tr>
<td height="5"><font color="#000000" size="2">总访问人数: <%=sessionC.getTotalVisit()%> </font></td>
</tr>
</table>
运行结果:
在线人数: 0 人
今日访问: 0 人
IP: 127.0.0.1
总访问人数: 0
无论怎么点击都无法改变