×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

请问一个关于JSP 与JAVABEAN中SESSION的问题

本文发表在 rolia.net 枫下论坛我用JSP做LOGIN等动作,监视HTTPSESSION,并有自己的用户级SESSION(比如连接数据库).我的目的是一旦侦察到浏览器关闭或做它用时,这个HTTPSESSION应该结束,而我的用户级SESSION也结束.
为此我在JAVABEAN中加入
public class myJavaBean implements HttpSessionBindingListener
{
.......
public void valueBound (HttpSessionBindingEvent sessionEvent)
{
...............
}

public void valueUnbound (HttpSessionBindingEvent sessionEvent)
{
// Session is closing, close the database connection
}
}
****************************************************************************
同时在JSP中我做以下动作:
<jsp:useBean id="myJavaBean" scope="session" class="myJavaBean" />
<% ....
session.setAttribute("myJavaBean",myJavaBean);
%>
****************************************************************************
但我执行的结果是:每次我关闭了浏览器后过约半小时,数据库的连接才断掉.请问我怎样才能做到一旦浏览器关闭或离开我的网页范围即关闭和数据库的连接呢?更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 请问一个关于JSP 与JAVABEAN中SESSION的问题
    本文发表在 rolia.net 枫下论坛我用JSP做LOGIN等动作,监视HTTPSESSION,并有自己的用户级SESSION(比如连接数据库).我的目的是一旦侦察到浏览器关闭或做它用时,这个HTTPSESSION应该结束,而我的用户级SESSION也结束.
    为此我在JAVABEAN中加入
    public class myJavaBean implements HttpSessionBindingListener
    {
    .......
    public void valueBound (HttpSessionBindingEvent sessionEvent)
    {
    ...............
    }

    public void valueUnbound (HttpSessionBindingEvent sessionEvent)
    {
    // Session is closing, close the database connection
    }
    }
    ****************************************************************************
    同时在JSP中我做以下动作:
    <jsp:useBean id="myJavaBean" scope="session" class="myJavaBean" />
    <% ....
    session.setAttribute("myJavaBean",myJavaBean);
    %>
    ****************************************************************************
    但我执行的结果是:每次我关闭了浏览器后过约半小时,数据库的连接才断掉.请问我怎样才能做到一旦浏览器关闭或离开我的网页范围即关闭和数据库的连接呢?更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • change HttpSession timeout value
      • 如果改变TIMEOUT的值,也会影响到浏览时的时间.我是允许他们在看网页时有半小时不更新的(总要允许他们离开一会吧).但我希望是在HTTPSESSION关闭时我的数据库连接也即时失效.
    • What you did works when the user does log out(request.getSession(false).invalidate()).
      What you want is still a puzzle now as far as I know. The client can close the browser arbitrarily while the server knows nothing about it, simply waiting the seession times out. Javascript Unload does not work here unfortunately. Sorry to give you bad news.
      • 那看来我只能跟用户讲:"一定一定要正常退出啊!" too bad :(
        • Not 2 bad although beans do consume some memory. The only bad thing is for Admin.
          When Admin is happy seeing some peopel online and wants to talk/communicate with those guys, they may be in lunch or in shopping.
          Mangage your resource carfully, don't keep a connection opening while doing nothing with it. Release it immidiatly after use it . Looks like your current design have some minor problems. Never trust clients, they always do something unexpected and keep trying to break down your application, hehe, same as me. good luck.
    • Close the database connection after every page of JSP code you are executing. Don't keep one connection opened for the life span of one session. That's VERY expensive. Imagine if you have
      1000 active sessions at some given time........
      • No, I can't disconnect database for per page. I have to trace their session,because every user has one order. 这是一个类似网上定货系统.
        • 你可以在session里保存他们订了什么,在最后结帐的时候一起进行数据库操作的
        • 查查有关shopping cart的demo code,很多,基本上都是放在session里,等最后check out时才真正放到DB里。