File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "session" Watch "session" New topic
Author

session

nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
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
joshua
Greenhorn

Joined: Jan 07, 2004
Posts: 2
[non-responsive post removed --bear]
[ March 07, 2004: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61608
    
  67

"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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61608
    
  67

Furthermore "joshua", if you do not have anything constructive to add to the topic, please refrain from replying.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61608
    
  67

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

Joined: May 14, 2002
Posts: 105
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
Mike Firkser
Ranch Hand

Joined: Oct 21, 2003
Posts: 247

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:


Mike Firkser
Rutgers '84
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
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


for (int i = today; i < endOfTime; i++) { code(); }
nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Hi winston
Thanks for your reply.I will try out your code .
bye
nitin
nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
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
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
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

Joined: Oct 12, 2000
Posts: 5093
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

Joined: May 14, 2002
Posts: 105
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

Joined: May 14, 2002
Posts: 105
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
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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 ]

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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

Joined: May 14, 2002
Posts: 105
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

Joined: Oct 12, 2000
Posts: 5093
closes just fine here.
Post the generated HTML...
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
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

Joined: May 14, 2002
Posts: 105
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

Joined: Oct 12, 2000
Posts: 5093
you never even get to see the page because you're not calling it on closing the other one.
nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Can u please correct my code.

bye
nitin
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093

remove the . in on.unload to get onunload
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
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

Joined: May 14, 2002
Posts: 105
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

Joined: May 14, 2002
Posts: 105
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
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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?


Groovy
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
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

Joined: May 14, 2002
Posts: 105
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session