为啥不能计数。能正常运行就是不能统计!

zhaohh 2004-12-23 12:07:31
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


无论怎么点击都无法改变
...全文
98 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaohh 2004-12-23
  • 打赏
  • 举报
回复
可还是不会变!
zhaohh 2004-12-23
  • 打赏
  • 举报
回复
相当于这样
/**
* 编写以下SessionCounter.java
* 并编译为SessiionCounter.class
* 然后放到你的网站的classpath的
* SessionCount(自己建立此目录)下面
*/
package com.oa.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();

totalVisit++;


}
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;
}

}
zhaohh 2004-12-23
  • 打赏
  • 举报
回复
我把它写成这样还是不行
/**
* 编写以下SessionCounter.java
* 并编译为SessiionCounter.class
* 然后放到你的网站的classpath的
* SessionCount(自己建立此目录)下面
*/
package com.oa.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);
*/
totalVisit++;
//activeSessions++;
/*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);

}//须写入一个文件中

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;
}

}
skylan 2004-12-23
  • 打赏
  • 举报
回复
把方法声明为static 的试试
ecaol 2004-12-23
  • 打赏
  • 举报
回复
在统计值得地方System.out.print();语句输出一下值,看看是多少,然后一个一个的找吧
pigo 2004-12-23
  • 打赏
  • 举报
回复


先把你那些对文件的操作的代码去掉再来运行。

81,092

社区成员

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

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