下面的代码改写自我写的用来回收资源用的一段代码。 怕泄漏公司秘密,不好意思,只能在其中加了一条打印语句, 不过大家可以根据需要添加业务逻辑。import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class ClearUpServlet extends HttpServlet implements ServletContextListener, HttpSessionListener { //Notification that the web application is ready to process requests public void contextInitialized(ServletContextEvent sce) { System.out.println("Start up the server"); } //Notification that the servlet context is about to be shut down public void contextDestroyed(ServletContextEvent sce) { System.out.println("Shut down the server"); } //Notification that a session was created public void sessionCreated(HttpSessionEvent se) { System.out.println("Newly created session:"+se.getSession().getId()); } //Notification that a session was invalidated public void sessionDestroyed(HttpSessionEvent se) { System.out.println("The session:"+se.getSession().getId()+" will be destroyed"); } }
import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class ClearUpServlet extends HttpServlet implements ServletContextListener, HttpSessionListener { //Notification that the web application is ready to process requests public void contextInitialized(ServletContextEvent sce) { System.out.println("Start up the server"); } //Notification that the servlet context is about to be shut down public void contextDestroyed(ServletContextEvent sce) { System.out.println("Shut down the server"); } //Notification that a session was created public void sessionCreated(HttpSessionEvent se) { System.out.println("Newly created session:"+se.getSession().getId()); } //Notification that a session was invalidated public void sessionDestroyed(HttpSessionEvent se) { System.out.println("The session:"+se.getSession().getId()+" will be destroyed"); } }怎么用啊?在页面中调用吗?
removeAttribute()方法只能删除会话中的属性不能而不能删除会话本身!
先用getSession()得到会话后可调用invalidate()方法中止!
如果客户关闭浏览器而不是正常注销,服务器无法发现这种情况!!!(jsp服务器其它的我不清楚)
我知道可以用session.getID()来获取每个会话的唯一ID但得到这个唯一ID后怎样得到特定会话就不清楚了,如果可以获取特定会话就可以用一个servlet or ejb监控会话以便及时关闭会话了!
只要浏览器全部关闭,好像SESSION就会自动销毁的--这要看session的超时了.
如果你在浏览器关闭时向服务器发送一段销毁的代码也可以.但执行的效果不理想
没有什么好的办法来判断,在关闭本页面的时候,用户有没有打开同一应用的其它页面,也就不能在页面的onclose时间中做一个提交,在处理页面中调用session.invalidate。
一个很笨的办法就是用Logout来代替,但并不是所有的用户都会点击这个按钮退出的。
怕泄漏公司秘密,不好意思,只能在其中加了一条打印语句,
不过大家可以根据需要添加业务逻辑。import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;public class ClearUpServlet
extends HttpServlet
implements ServletContextListener, HttpSessionListener {
//Notification that the web application is ready to process requests
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Start up the server");
} //Notification that the servlet context is about to be shut down
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Shut down the server"); } //Notification that a session was created
public void sessionCreated(HttpSessionEvent se) {
System.out.println("Newly created session:"+se.getSession().getId()); } //Notification that a session was invalidated
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("The session:"+se.getSession().getId()+" will be destroyed");
}
}
其实上面的代码只是用来统计在线人数的化,我想Sun公司也就不用加这两个事件了。
我现在用到的是:在destroy中除去临时、无用的资源(象临时文件,临时表中的数据可能并不能在用完之后就删除。像报表,可能生成很多页,你不知道什么时候在用那一页,此时在session destroy事件中删除就是一个不错的主意)。当然在服务器启动的时候,你可以在create事件中预先加载一些资源,进行一些初始化操作。我想合理利用这些事件,用处还是蛮大的
“没有什么好的办法来判断,在关闭本页面的时候,用户有没有打开同一应用的其它页面,也就不能在页面的onclose时间中做一个提交,在处理页面中调用session.invalidate。“我们怎么处理这种情况?
当客户的浏览器关闭时,并不向服务器提交关闭请求,这样只能等到
session自然超时的时候,Tomcat才清除该session相关的信息。
============================================
这话有理阿!
看了半天,还是
“还有一个很色情的办法就是用色情网站上常用的方法,关闭该页后弹出另一页,在另一页里自动执行销毁session来代替手工销毁,然后自动关闭这个小的窗口。代码简单而实惠”这个办法最有实际意义,不过客户端浏览器关闭后
服务器端的SESSION真的没用了吗?
在应用了COOKIE的网站里也是重新建SESSION而不是取回上一次连接的SESSION?
如果是的话,那说明HTTP协议的安全性还是不错的
否则~~
import javax.servlet.*;
import javax.servlet.http.*;public class ClearUpServlet
extends HttpServlet
implements ServletContextListener, HttpSessionListener {
//Notification that the web application is ready to process requests
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Start up the server");
} //Notification that the servlet context is about to be shut down
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Shut down the server"); } //Notification that a session was created
public void sessionCreated(HttpSessionEvent se) {
System.out.println("Newly created session:"+se.getSession().getId()); } //Notification that a session was invalidated
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("The session:"+se.getSession().getId()+" will be destroyed");
}
}怎么用啊?在页面中调用吗?