aspose file tools*
The moose likes Servlets and the fly likes Tabs within one browser share Session, but I want to capture close and time out event for each tab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tabs within one browser share Session, but I want to capture close and time out event for each tab" Watch "Tabs within one browser share Session, but I want to capture close and time out event for each tab" New topic
Author

Tabs within one browser share Session, but I want to capture close and time out event for each tab

Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

Hi friends!

I just learned that multiple tabs within one browser window share the same http session, therefore I need to make some changes on my projects.
My client side is Applet loading in a html page, and server is Servlet.

Now my questions are:
1: Is it possible for me to capture tab close event at server side? Something similar to session terminate.
2: Is it possible for me to assign a max-inactive-interval to one tab and capture the time-out event?
3: Can I ensure if the session is still alive, the tab closing event captured by client, can be sent to server successfully?

I guess if I'm able to solve above problems, I don't need to use URL encoding (I'm not familiar with that either) to make the server consider two tabs as two sessions.

Thanks in advance! It's my first time to post something

Estillan


My first program "Hello World" was in 1997, but until now that's the only thing I know.
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Hi Xu, I donot think you can capture the event related to Tabs. As tabs are the property of the browser and it shares the session data. So you cannot diff. events between tabs.

Regards,
Sunil
Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

Hi Kumar,

Thanks for your reply.

Is it correct I have two options:

either implement the tab time-out event by myself, for example, make the client sending heart beat message to the server, and server update its status;
or try to find the solution to make each tab gets one session for its own?

Estillan
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

I believe, configuring a session timeout value in your web-application can solve this issue. However, if you want to keep the session alive, you can use AJAX to send heart beat signals back to the server to update the session timeout interval.


S.D. MADHAN
Not many get the right opportunity !
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Hi Devaki,

What ever solution you have suggested is applicable to entire session. It is not related to Your tab event. Session timeout in web application is valid for all tabs. Ajax call will increase the session but it again for all tags.

@Estillan - i am not very much sure how you can control tab event with Server. You can write your own events that will be fired on closing the Tab but again it will be common to all open tabs from Server point of view.

Tabs are just replica of main window. It shares same data with same session id.

If you get any answer, please post it. It will be useful for other's.

Regards,
Sunil
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

See if the following link helps you:

http://stackoverflow.com/questions/368653/how-to-differ-sessions-in-browser-tabs


Java Experience
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I have to ask: why do you need to subvert the session behaviour of the browser in this way?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

Paul Sturrock wrote:I have to ask: why do you need to subvert the session behaviour of the browser in this way?


Hi Paul,

You can imagine that each tab is one instance of my application, and user may want to open several instances of the application. Each instance corresponds to one particular entry in the Session. I want to capture the time-out (the tab crash but session is till alive... is it possible? ) and terminate (user close the tab) event to let server clear up that entry and do other actions.

That's why initially I have no idea multiple tabs belong to one session, I was trying to use http session API. Of course another solution is to force user open a new browser window each time in order to have a new session (that's not always true, for Chrome, even multiple windows belong to one session. )

I hope this explanation is clear.

Estillan
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes Estillan, that makes sense. But it does sounds like you are making work for yourself. Do users actually need to open many instances of the application? Its just I can think of several good reasons why you probably wouldn't want this behaviour.
Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

For example, user may want to use Google Doc to open multiple files.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not sure why this would need you to open multiple instances of something, just multiple docs in one application. Am I missing something?

Consider the other side, if a user is using two concurrent instances of one database backed application (for example) and working on closely related or the same data through both they will run the risk of either getting lost updates caused or optimistic locking issues they've caused. Its an extra risk to (as far as I can see) no gain.
Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

My application is written in Java Applet embedded in web page.
If user wants to open multiple files, multiple applet instance need to be loaded.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

But Applets are client side - what part does the Session play in that? (again - I may be being really stupid here - but this sounds like its more complex than it needs to be)
Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

I'm sorry. My bad. Maybe I didn't sketch it clearly.

The client is making http request to the server. The session is used here for two reasons:

1: to store client information at server side (in order to reduce the traffic and enhance the security).

2: for server to notice when client quit or inactive for a while.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That's all OK. But why a distinct session per Applet? Would it not be easier to uniquely identify each Applet instance and partition session data based on this? Is that not a bit easier than trying to subvert or workaround expected browser behaviour?
Estillan Xu
Greenhorn

Joined: Apr 05, 2011
Posts: 12

True. Back to my initial question, the reason why I'm trying to "assign" session for each Applet is that I want to use HTTP Session API such as "setMaxInactiveInterval", "SessionBindingListener", etc. to monitor the behavior of each Applet. However, if I cannot achieve this, I will implement those function to handle each Applet by myself then.

For example, I'll use "heart-beat" to detect Applet time-out.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What would this heart beat do?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Tabs within one browser share Session, but I want to capture close and time out event for each tab