• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys
Im developing a website .I need to capture the event when the user closes his browser window and redirect to the starting page.I know that we use something called HttpSessionListenerBinding .Can anyone post me the snippet of code
bye
Nitin
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[non-responsive post removed --bear]
[ March 07, 2004: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"joshua",
You have already received one warning (in this topic) to adjust your display name according to the JavaRanch Naming Policy. Please change your display name to match it prior to your next post. Accounts with invalid names are subject to removal.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
bear
JSP Forum Bartender
[ March 07, 2004: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Furthermore "joshua", if you do not have anything constructive to add to the topic, please refrain from replying.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now as to the question...
While it is true that a listener can tell you that the session has expired or has been invalidated, this will not happen as a result of the browser being closed. In fact, there is no reliable way for the server to know that the user has closed the browser.
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to close my session when the user closes his browser window.Even also I need to know when the session is time out. For this Its said that i ned to configure in web.xml in the jsp .Do u have any idea of this.Please can anyone send me a snippet of code
bye
nitin
 
Ranch Hand
Posts: 249
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to time out a session due to user inactivity probably depends on the server you're using. We use WebLogic and have the following in our web.xml file:
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ongoing struggle to detect and interpret client actions continues. I've run into the Timeout Issue several times. For instance, we needed to perform some clean-up when the user was no longer active (i.e. logged off, session expired). Well, this is rather easy, if the user follows the PROPER logout procedure, however, as we all know, the quintessential web client is practically an expert at doing everything IMPROPER. So, the question is, how to detect the browser close.
We used some javascript and a function call on each page, embedded in the BODY tag as follows:

Now, the page browserExitExpire.jsp is rather simple:

In a nutshell, each time the user leaves a page, this "utility" page is called, and the maxInactiveInterval is set to 10 seconds. If the server is not contacted within 10 seconds (i.e. with another page request) the session will be invalidated. So, if the user exits the browser, this page will be called before the current page is unloaded, effectively creating a 10-second "monitor" which will expire and hence the server may perform "clean up" duties.
This is not a very elegant hack, but, who can afford to be elegant anymore (time = $$$).
WS
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi winston
Thanks for your reply.I will try out your code .
bye
nitin
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi winston,
I have tried out your code and placed the body tag in every jsp.But when Im closing the window its calling the jsp page (<BODY onbeforeunload="location.href=' <%= response.encodeURL("abc.jsp")%> '"> but the explorer window is not closing and its flikering I dont know why.Could you please help me out.I have included the javascript and setsessionmaxInactive(10) Interval in abc.jsp.
bye
nitin
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not place the code to call the close page inside that page, which I think you've inadvertently done).
Otherwise the close page will call itself in what's effectively an eternal loop which can hardly be as intended.
 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
I've brushed the dust off the old program where I used this technique and re-tested the code and it works. You may have a syntax error somewhere in your href. Also, this was developed in IE, so I'm not sure about cross-browser compatability here. If all else fails, post your code here, and I'll take a look.
WS
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
might need to be (and would certainly be nicer code )

Had to put a space in onload to get the forum engine to accept it...
Some browsers are (correctly IMO) picky about HTML syntax and following correct constructs should never be omitted (XHTML makes the head and title tags mandatory, the html and body tags already were).
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi winston ,
I suppose I did not call like that as u said. I had a page called header.jsp in which I included the body tag which calls the abc.jsp.
Im using browser as internet explorer.

nitin
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi winston Im pasting the abc and abcexpire.jsp Im using IE 5.0

/abc.jsp///////////////////////////////////////////////////////////
<%@ page language="java" session="true" isThreadSafe="true" contentType="text/html; charset=ISO-8859-1" %>
<%out.println("abc.jsp");%>
<html>
<head>
<title>
</title>
</head>
<BODY onbeforeunload="location.href='<%= response.encodeURL("abcexpire.jsp") %>'">
</BODY>
</html>
///////////////////////////////////////////////////////////////////////////
abcexpire.jsp

<%@ page language="java"
session="true"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<%session.setMaxInactiveInterval(10);%>
<http>
<body>
<SCRIPT language="Javascript"> window.close();</SCRIPT>
</body>
</http>

///////////////////////////////////////////////////////////////////////////
Can u please tell me why abc.jsp is not closing when I cose the browser window

bye
nitin
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
This is because, closing the windows does not mean you have called the JS window.close().
I think you should change your code to:

JS will not be called unless you do some actions.
Otherwise, all JS inside a page will be called when the HTML is loaded.
Nick.
[ March 11, 2004: Message edited by: Nicholas Cheung ]
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that the attribute "on load" should read as "onload".
I add a space in between because I cannot post the message if I use the whole word "onload" within my post together with HTML tag.
Nick.
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi cheung,
Im still getting the same error.My browser window is not closing.Can u please tell where the error is
<%@ page language="java"
session="true"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<%session.setMaxInactiveInterval(10);%>
<html>
<body on load="javascript:window.close();"></body>
</html>
nitin
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
closes just fine here.
Post the generated HTML...
 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin, let me just clarify this to make sure you're using the pages correctly. The page, abcexpire.jsp, is the page you will close manually, by closing the browser window. Now, what will happen, is a request will be sent to the server for the second page (the page making the call to window.close(), call it expireSession.jsp). In effect, this second request will set the maximum inactive interval for your session to 10 seconds. So...you have closed abcexpire.jsp, ending your session -- but the server doesn't know this. What is does know, however, is that if your session id does not make another request in 10 seconds, it will end your session on the server-side.
The window.close() call in expireSession.jsp will, most times, execute fast enough so the client never sees this page come up. Again, not very elegant, but it'll do the job.
Good luck,
WS
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi winston,
<%@ page language="java"
session="true"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<%out.println("abc.jsp");%>
<html>
<head><title></title></head>
<BODY onbeforeunload="location.href='<%= response.encodeURL ("abcexpire.jsp") %>'">
</BODY>
</html>

this is abc.jsp which I close manually
**********************************************************************
abcexpire.jsp is called frm abc.jsp
<%@ page language="java"
session="true"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<%session.setMaxInactiveInterval(10);%>
<html>
<body on load="javascript:window.close();"></body>
</html>
************************************************************************
These are the files I use.But still I get the problem
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you never even get to see the page because you're not calling it on closing the other one.
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can u please correct my code.

bye
nitin
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

remove the . in on.unload to get onunload
 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you got the pages mixed up? Try this example:

Now, the code above is for testPage.jsp. When you close testPage.jsp, a request will be sent to the server for the following page:

So, again, the page you will close is testPage.jsp. You do nothing with pageExpire.jsp. This code works for me, so I can't really tell you what's wrong other than you may be closing the wrong window. Good luck,
WS
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi joreng,
Your code seems to satisfy my problem 75 %.
<body on.unload="window.open('abcexpire.jsp');">
Now abc.jsp is not flickering when I close the browser window and its able to forward to abcexpire.jsp.
But abcexpire.jsp does not seem to close.Even though I use javascript.
I also want to know that can we capture the browser events such as closing,moving back forward.Bcoz Im maintaing a table and only when user closes the browser window I need to update the database table to avoid duplicate login.
/***************************abc.jsp***********************************/
<%@ page language="java"

isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<%out.println("abc.jsp");%>
<HTML>
<HEAD>
<TITLE>testPage.jsp</TITLE>
</HEAD>
<BODY on unload ="window.open('abcexpire.jsp');">
heLLo worLd. </BODY>
</HTML>
/************************abcexpire.jsp********************************/
<%@ page language="java"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<% session.setMaxInactiveInterval(10);%>
<html>
<body on load="javascript:window.close();"></body>
</html>
bye
nitin
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys ,
Can anyone help me with code for capturing browser events in a jsp page or javascript such browser closing ,moing back
etc
bye
nitin
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In a nutshell, each time the user leaves a page, this "utility" page is called, and the maxInactiveInterval is set to 10 seconds. If the server is not contacted within 10 seconds (i.e. with another page request) the session will be invalidated. So, if the user exits the browser, this page will be called before the current page is unloaded, effectively creating a 10-second "monitor" which will expire and hence the server may perform "clean up" duties.


I dont understand why do we need utility page, wont just session time out (HttpSessionBindingListener) suffice?
 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The HttpSessionBindingListener can detect the timeout event, but, the problem is, when the user closes his/her browser window, the server has no idea that the session should be ended. In most cases, a server only knows that a session should be ended when the session is explicitly ended by a server-side utility, or a timeout event occurs.
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi joreng,
Your code seems to satisfy my problem 75 %.
<body on.unload="window.open('abcexpire.jsp');">
Now abc.jsp is not flickering when I close the browser window and its able to forward to abcexpire.jsp.
But abcexpire.jsp does not seem to close.Even though I use javascript.
I also want to know that can we capture the browser events such as closing,moving back forward.Bcoz Im maintaing a table and only when user closes the browser window I need to update the database table to avoid duplicate login.
/***************************abc.jsp***********************************/
<%@ page language="java"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<%out.println("abc.jsp");%>
<HTML>
<HEAD>
<TITLE>testPage.jsp</TITLE>
</HEAD>
<BODY on unload ="window.open('abcexpire.jsp');">
heLLo worLd. </BODY>
</HTML>
/************************abcexpire.jsp********************************/
<%@ page language="java"
isThreadSafe="true"
contentType="text/html; charset=ISO-8859-1" %>
<% session.setMaxInactiveInterval(10);%>
<html>
<body on load="javascript:window.close();"></body>
</html>
bye
nitin
reply
    Bookmark Topic Watch Topic
  • New Topic