tags:

views:

590

answers:

4

I was wondering, How would I do session.remove("userBean") in JSF after user closes the window (hitting 'X' of browser) on JSF 1.2.

I was thinking to put a confirmation pop up after user hits 'X' of browser but not quite sure how do I access or bind the Ok button to my defined method, where I have that logic to clear the scoped bean. Or is there any better approach of doing this?

Any help would be highly appreciated.

Thanks.

+1  A: 

Why you want to remove an attribute. In fact, when the session will be invalidated all would do garbage collected anyway, since there would be no reference. You might want to use a low timeout in this case.

In case you want to invalidate session right away. You can always employ a JavaScript function to post the request to your server, on a click of your "Ok" button. This might not be a very reliable thing to do, but wouldn't hurt as a facility. Below is an example code,

JavaScript Function

function show_confirm()
{
  var r=confirm("Are you sure you want to abort the session?");
  if (r==true) {
    // set an appropriate form action and 
    document.form.submit();
  }
}

HTML

<input type="button" onclick="show_confirm()" value="Show confirm box" />

[Edited after your comments]

So, you want to bind the button to your bean method, you have to do something custom, using CSS and JavaScript. Look at this tutorial, it will provide you some insights on how you will create a custom popup. Since its just a normal <div>, you would be doing it just like any other button, no rocket science here, just put your button code inside that <div>.

Adeel Ansari
How can I employ a JavaScript function to post the request to my server, on a click of a "Ok" button. Could you please provide some example as I am newbie.Thanks,
I mean you can make use of a confirm box. See my addendum.
Adeel Ansari
But can't I bind the ok button to my method from bean class, I mean not javaScript method.Kindly suggest.
No no. Not at least with the native confirm dialog.
Adeel Ansari
+1  A: 

You can use MyFaces Orchestra to define your beans in the custom conversation.access and conversation.manual. They will allow your beans to be removed more recently than if they were stored in session.

  • you can define a timeout for a conversation, which is different (and smaller) from the session timeout.
  • if using conversation.access, your beans will be removed immediately after they are no longer referenced by a user request - i.e. the conversation has ended. This doesn't apply to your case (closing the browser), but is still good to know.

Conversations are sequences of steps performed by the user belonging to the same logical context - i.e. the whole registration process is one conversation, although it spans multiple pages.

Generally, you don't have to worry about what remains in the session after the user closes it. It will eventually be timeouted and garbage collected. You have to worry about what's in the session while he is browsing, because it can grow very big.

Bozho
+1  A: 

In JSF, you will be making something like

JSF page:

<h:commandButton id="closeBtn"
      type="submit" value="Close" onclick="return showConfirmClose();" 
      actionListener="#{backBean.cleanUpMethod}"
</h:commandButton>

Javascript:

 //in case the user clicked No, this method should return false,
 //preventing the action from being submitted, but double check all the return values first 

 function showConfirmClose(){   
     var c = confirm("Are you sure?");
     return c;    
 }

BackBean:

public void cleanUpMethod(ActionEvent event){
      //do cleaning here
}
medopal
+1  A: 

I was wondering, How would I do session.remove("userBean") in JSF after user closes the window (hitting 'X' of browser) on JSF 1.2.

You don't need to worry about it. If the session times out, then the bean will surely be removed. You can if necessary control the session timeout period in <session-config> in web.xml. If you actually want to hook on the session timeout/destroy, then you can just implement HttpSessionListener and intercept on sessionDestroyed(), or let UserBean implement HttpSessionBindingListener and intercept on valueUnbound().

If you really insist in listening on the browser close button, which is quite much more work after all, then all you can do is to use the Javascript's beforeunload event. There are no other ways. You should only keep in mind that this is an unstandard event and that this is thus not per se supported by all webbrowsers. E.g. Opera doesn't support this event. It's quite a work to get this streamlined, because this event would also be fired when you do a plain vanilla navigation or a form submit, you'll need to suppress this event on every "normal" link/button. You'll also need to fire an Ajaxical request to inform the server about the close, because you cannot change the page location or submit a hidden form anymore as the browser is going to be closed.

BalusC