1

Is it possible to call Bean from Jquery? My requirement is like below,

I have a JSF 1.2 based Servlet.

Am invalidating a user's session if he is idle for some time. Am showing Jquery dialog box 1 minute before invalidating the session. A user has 2 options in the dialog box. "Yes I want to continue" will extend the session. "No I want to logout" will logout the user.

When user clicks on "No I want to logout", I want to call bean method where I update the Database & invalidate the session.

Below is the code,

'No, Log out': function(){
 $j.idleTimeout.options.onTimeout.call($j.post('//This is where am stuck',function()

I want to call bean in $j.post so that I can do some clean up activities in my bean.

How this can be done?

Regards,

Mark
  • 28,783
  • 8
  • 63
  • 92
Vikas V
  • 3,176
  • 2
  • 37
  • 60
  • What if the enduser clicks "Yes" *after* the dialog is been shown for 1 minute? – BalusC Sep 24 '12 at 11:01
  • @BalusC: When user clicks on "Yes, I want to continue", am calling one JSP page (extend_session.jsp). This JSP page has nothing inside it. So, when this JSP page is hit by user (before session invalidation triggers) , the session gets extended. In this way, a user can continue browsing. If user doesn't click on "Yes", withing 1 minute session invalidation will happen and user will be redirected to login page. – Vikas V Sep 24 '12 at 11:09
  • You said that the dialog is shown 1 minute before session invalidation. So if the dialog is been shown for *more* than 1 minute (e.g. because the enduser has been out for lunch or toilet or smoke or so), then session is already invalidated in the server (at least, you are implying that). – BalusC Sep 24 '12 at 11:10
  • @BalusC : Session timeout set in my Server is 15 mins. Dialog box will be shown at 14th min and it will start counter which ticks from 59 to 1. If a user doesn't click on "Yes", and when the counter value becomes 0, it means it reached 15mins and Session invalidation will happen from Server. I have written Listeners (HttpSessionListener) which gets invoked. Here, am handling clean up code (like calling DB etc) and finally redirecting to login page again. – Vikas V Sep 24 '12 at 11:15

2 Answers2

1

You can either use just use a hidden(style="display:none") commandButton with an action pointing to a method in your bean , and call a .click() on it from jquery

something like this

<h:commandButton id="myButton" action="#{myBean.myInvalidateMethod}" style="display:none"/>

jquery

&("#myButton").click();//possible myForm prefix appear before the id so use #myForm\\:myButton selector

Or you can call servlet from your jsf page , similar to this answer Calling a Servlet from a JSP page using jQuery Ajax

Community
  • 1
  • 1
Daniel
  • 36,833
  • 10
  • 119
  • 200
0

You can't directly access the methods, you'll have to make your servlet handle your request and call the method for you and return the data in json format for example

Asciiom
  • 9,867
  • 7
  • 38
  • 57