onbeforeunload and servlets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek

    #1

    onbeforeunload and servlets

    Hello:

    Can anybody tell me how to detect browser close event in Javascript and how
    to call a servlet on a Brower close?

    I tried some thing:

    <html>
    <SCRIPT LANGUAGE="JavaS cript1.2" TYPE="text/javascript">

    function unloadMess(){
    mess = "message."
    return mess;
    }

    function setBunload(on){
    window.onbefore unload = (on) ? unloadMess : null;
    location.href=" http://localhost/servlet/ServletPrueba";
    }

    setBunload(true );

    </SCRIPT>
    <BODY>
    <a href="http://www.microsoft.c om" onClick="setBun load(true)">Lin k 1</a>
    <br>
    <a href="http://localhost:8080/Version3/pagina2.html"
    onClick="setBun load(false)">Li nk 2l</a>
    </BODY>
    </html>

    But, I want call the servlet only when close browser or I browse to another
    web.

    Thanks.


  • ExGuardianReader

    #2
    Re: onbeforeunload and servlets

    Derek wrote:
    [color=blue]
    > Hello:
    >
    > Can anybody tell me how to detect browser close event in Javascript and how
    > to call a servlet on a Brower close?
    >
    > I tried some thing:
    >
    > <html>
    > <SCRIPT LANGUAGE="JavaS cript1.2" TYPE="text/javascript">
    >
    > function unloadMess(){
    > mess = "message."
    > return mess;
    > }
    >
    > function setBunload(on){
    > window.onbefore unload = (on) ? unloadMess : null;
    > location.href=" http://localhost/servlet/ServletPrueba";
    > }
    >
    > setBunload(true );
    >
    > </SCRIPT>
    > <BODY>
    > <a href="http://www.microsoft.c om" onClick="setBun load(true)">Lin k 1</a>
    > <br>
    > <a href="http://localhost:8080/Version3/pagina2.html"
    > onClick="setBun load(false)">Li nk 2l</a>
    > </BODY>
    > </html>
    >
    > But, I want call the servlet only when close browser or I browse to another
    > web.[/color]

    You are trying to "close down" your servlet session to release resources
    when the user terminates the browser, or navigates away?

    There's no *reliable* way of doing this, but where I work, we've had to
    do this because of "per seat" licensing in the back end, and the
    connection which is maintained in the servlet session. (Yes, it's
    horrible, I inherited it, the back end server maintains a huge amount of
    state for each connection, and they don't want to set it up and tear it
    down for every request)

    We've had to do something VERY ugly...

    The application is loaded into a frameset, so the front page just
    contains (bear with me, I've forgotten the attributes...)

    <frameset ununload="windo w.open('logout. do','','height= 100,width=100') "
    frameborder="no " rows="*">
    <frame src="TheREALfro ntpage.jsp" border="0">
    <noframes>Tough ! We've implented an ugly hack</noframes>
    </frameset>

    All subsequent pages load into the frame, so the unload event isn't
    fired until you close thr browser of navigate that window away.

    So, you get a little window popping up when you close the browser or
    navigate away.

    The logout servlet must send

    <script type="text/javascript">
    self.close();
    </script>

    which then gets rid of it.

    Of course this is riddle with flaws. If the user opens any of the links
    in the application in a new window, these don't have that extra frame.
    The use may close the original frame which will log out, and invalidate
    the session. The newly opened windows will be orphaned.

    Yuck. Horrible. I've tried to implement all kind of other ways.

    One way was not using the frame and having *every* page in the app fire
    a logout request on unload. The servlet would flag a request for another
    page, and ignore that logout if another page in the app was being requested.

    Well, on a page *refresh", the onunload is fired before the request is sent.

    There is no good way to do this.

    Nige

    Comment

    Working...