我的办法是用xml文件来纪录,很方便。
解决方案 »
- 求助spring注入的对象是default的,没有设为私有的,会不会报空指针异常
- 急问:JSP实现文件上传,浏览器中支持大文件和断点续传
- ■■■■■■ 赠送JSP空间+SQL,不限大小,符合条件的人请看帖!!■■■■■■
- 有没有会JSF的高手 帮忙看点东西啊。
- 刚学JSP,做了个下载的程序。可下载下来的东西却是JSP页面。大虾进来帮帮忙啊
- 文字排版问题,请各位高手帮助!急急急急急急急急急急急急急急急急急急急急急急急急急!
- HttpURLConnection 请求鉴权,求指点
- 谁有jsp实现的权限控制系统啊!
- JSP里sql句子select的使用问题~~~
- 怎么让我的服务器同时去持ASP与JSP?
- 求树的遍历
- java与数据库的问题
import java.util.*;
import javax.servlet.http.*;//监听登录的整个过程
public class SessionListener implements HttpSessionBindingListener
{public String privateInfo=""; //生成监听器的初始化参数字符串
private String logString=""; //日志记录字符串
private int count=0; //登录人数计数器public SessionListener(String info){
this.privateInfo=info;
}public int getCount(){
return count;
}public void valueBound(HttpSessionBindingEvent event)
{
count++;
if (privateInfo.equals("count"))
{
return;
}
try{
Calendar calendar=new GregorianCalendar();
System.out.println("LOGIN:"+privateInfo+" TIME:"+calendar.getTime());
logString="\nLOGIN:"+privateInfo+" TIME:"+calendar.getTime()+"\n";
for(int i=1;i<1000;i++){
File file=new File("yeeyoo.log"+i);
if(!(file.exists()))
file.createNewFile(); //如果文件不存在,创建此文件
if(file.length()>1048576) //如果文件大于1M,重新创建一个文件
continue;
FileOutputStream foo=new FileOutputStream("yeeyoo.log"+i,true);
//以append方式打开创建文件
foo.write(logString.getBytes(),0,logString.length()); //写入日志字符串
foo.close();
break;//退出
}
}catch(FileNotFoundException e){}
catch(IOException e){}
}public void valueUnbound(HttpSessionBindingEvent event)
{
count--;
if (privateInfo.equals("count"))
{
return;
}
try{
Calendar calendar=new GregorianCalendar();
System.out.println("LOGOUT:"+privateInfo+" TIME:"+calendar.getTime());
logString="\nLOGOUT:"+privateInfo+" TIME:"+calendar.getTime()+"\n";
for(int i=1;i<1000;i++){
File file=new File("yeeyoo.log"+i);
if(!(file.exists()))
file.createNewFile(); //如果文件不存在,创建此文件
if(file.length()>1048576) //如果文件大于1M,重新创建一个文件
continue;
FileOutputStream foo=new FileOutputStream("yeeyoo.log"+i,true);
//以append方式打开创建文件
foo.write(logString.getBytes(),0,logString.length()); //写入日志字符串
foo.close();
break;//退出
}
}catch(FileNotFoundException e){}
catch(IOException e){}
}}登录日志的实现: 下面再来看看我们的登录Servlet中使用这个监听器的部分源代码: ……
HttpSession session = req.getSession (true);
……
//////////////////////////////////////////////////////////////////
SessionListener sessionListener=
new SessionListener("IP:"+req.getRemoteAddr());
//对于每一个会话过程均启动一个监听器
session.setAttribute("listener",sessionListener);
//将监听器植入HttpSession,这将激发监听器调用valueBound方法,
//从而记录日志文件。
//////////////////////////////////////////////////////////////////当系统退出登录时,只需简单地调用session.removeAttribute(“listener”); 即可自动调用监听器的valueUnbound方法。或者,当Session Time Out的时候也会调用此方法。 登录人数的统计: ServletContext session1=getServletConfig().getServletContext();
//取得ServletContext对象实例
if((SessionListener)session1.getAttribute("listener1")==null)
{
SessionListener sessionListener1=new SessionListener("count");
//只设置一次,不同于上面日志文件的记录每次会话均设置。
//即当第一个客户连接到服务器时启动一个全局变量,
//此后所有的客户将使用相同的上下文。
session1.setAttribute("listener1",sessionListener1);
//将监听器对象设置成ServletContext的属性,具有全局范围有效性,
//即所有的客户均可以取得它的实例。
}
session.setAttribute("listener1",(SessionListener)session1.
getAttribute("listener1"));
//取出此全局对象,并且将此对象绑定到某个会话中,
//此举将促使监听器调用valueBound,计数器加一。在此后的程序中随时可以用以下代码取得当前的登录人数: ((SessionListener)session.getAttribute("listener1")).getCount()getCount()是监听器的一个方法,即取得当前计数器的值也就是登录人数了。