session 超时的问题 当然可以,你可以用sessionlisterner 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这里有一个例子,有用记得加分。import javax.servlet.*; import javax.servlet.http.*; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent se) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent se) { if(activeSessions > 0) activeSessions--; } public static int getActiveSessions() { return activeSessions; } } 然后需要在你的WEB-INF中建立web.xml 文件内容如下: <!-- Web.xml --> <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd"> <web-app> <!-- Listeners --> <listener> <listener-class> SessionCount.SessionCounter </listener-class> </listener> </web-app> 我想要一个功能,就是在jsp上显示用户还有多长时间用户的session就会过期,有什么办法可以实现吗? to Joeblackyang(野Heart) : 我只需要用这个servlet监听就可以了吗?如果我要在session 断开的时候调用一个方法,就是在sessionDestroyed()方法里调用了,对不对?等你回复,谢谢 有个方法是设置session存活时间的,好好找找了 设置session的时间很简单,现在主要是session 超时的时候,我要写日志,不知道怎么做 HttpSession session = request.getSession(true);if (session.isNew()){} binha: 不知道你的这个用法什么意思? HttpSession session = request.getSession(false);if (session == null){ 写日志} 我写了一个监听的servlet,但是,当一个session超时的时候没有监听到,不知道为什么?package examples.servlets;import javax.servlet.*; import javax.servlet.http.*; import java.util.*;import java.io.*;public class SessionCounter extends HttpServlet implements HttpSessionListener { private static int activeSessions = 0; HttpSession sess = null; ObjectOutputStream out = null; ObjectInputStream in = null; public static Hashtable h = new Hashtable(100); public void sessionCreated(HttpSessionEvent evt) { //activeSessions++; sess = evt.getSession(); log("\nSession created: Id: " + sess.getId() + " CreationTime: " + sess.getCreationTime() ); synchronized(this) { h.put(sess.getId(), sess); } log("Number of active sessions: " + h.size()); } public void sessionDestroyed(HttpSessionEvent evt) { sess = evt.getSession(); log("\nsession destroyed: " + sess.getId() ); synchronized(this) { h.remove(sess.getId()); } log("Number of active sessions: " + h.size()); } public static int getActiveSessions() { return activeSessions; } public void log(String s){ System.out.println("[SessionListener] " + s); } } 当我网页之间传递的session,通过HttpSessionListener 是否可以监听到?请高人指教 session和线程有关,可以记录线程ID Joeblackyang(野Heart) 的例子已经回答了你的问题。别忘了配置web.xml还有HttpSessionListener接口没必要实现在servlet上。------------------------------------------------------------------当我网页之间传递的session,通过HttpSessionListener 是否可以监听到?这个问题你问得不太明白。除了HttpSessionListener外,还有HttpSessionAttributeListener和HttpSessionActivationListener接口应该能够满足你需求。 to xs21cn:我都配置了,在这里我继承servlet是想从客户端取的参数,然后写日志。当监听的时候,有没有办法取到客户端的参数? Java 运行不了,出现"Error: could not find java.dll" 怎么解决? 关于类似QQ软件的询问 急求助,希望实现一个JLabel可以在拖动,就象在 jbuilder 开发UI 组件时可以任意拖动 并改变大小 paint()与paintComponent()有何区别?来者有分!! 【初级】怎么样正确的使用java中的package??? applet在ie里什么都没有,都是灰色的了。。。。 C算法转为java算法的问题 编译程序 ?在线等待 关于线程的问题 一个粗浅的问题,请赐教。 jbuilder7下载!!!!
import javax.servlet.http.*;
public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0;
public void sessionCreated(HttpSessionEvent se) {
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0) activeSessions--;
}
public static int getActiveSessions() {
return activeSessions;
} } 然后需要在你的WEB-INF中建立web.xml
文件内容如下: <!-- Web.xml -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
<!-- Listeners -->
<listener>
<listener-class>
SessionCount.SessionCounter
</listener-class>
</listener>
</web-app>
我只需要用这个servlet监听就可以了吗?如果我要在session 断开的时候调用一个方法,就是在sessionDestroyed()方法里调用了,对不对?
等你回复,谢谢
if (session.isNew()){}
不知道你的这个用法什么意思?
if (session == null){
写日志
}
package examples.servlets;import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
public class SessionCounter extends HttpServlet implements HttpSessionListener {
private static int activeSessions = 0;
HttpSession sess = null;
ObjectOutputStream out = null;
ObjectInputStream in = null; public static Hashtable h = new Hashtable(100);
public void sessionCreated(HttpSessionEvent evt) {
//activeSessions++;
sess = evt.getSession();
log("\nSession created: Id: " + sess.getId() + " CreationTime: " + sess.getCreationTime() );
synchronized(this) {
h.put(sess.getId(), sess);
}
log("Number of active sessions: " + h.size());
}
public void sessionDestroyed(HttpSessionEvent evt) {
sess = evt.getSession();
log("\nsession destroyed: " + sess.getId() );
synchronized(this) {
h.remove(sess.getId());
}
log("Number of active sessions: " + h.size());
}
public static int getActiveSessions() {
return activeSessions;
}
public void log(String s){
System.out.println("[SessionListener] " + s);
}
}
别忘了配置web.xml
还有HttpSessionListener接口没必要实现在servlet上。
------------------------------------------------------------------
当我网页之间传递的session,通过HttpSessionListener 是否可以监听到?这个问题你问得不太明白。除了HttpSessionListener外,还有HttpSessionAttributeListener和HttpSessionActivationListener接口
应该能够满足你需求。
我都配置了,在这里我继承servlet是想从客户端取的参数,然后写日志。
当监听的时候,有没有办法取到客户端的参数?