Set Session Time out programatically

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Set Session Time out programatically

rajeev_yadav
Hi All,

My requirement is that I want to set session timeout in application programatically. I know it can be done through web.xml but due to some env constraints, we have to avoid setting through web.xml.

Problem statement:
Check if user is inactive in application for certain time period(lets consider 15 min) than invalidate session. Inactive period should include ajax calls as well so if user has made any ajax/normal call within 15 min than session should not be invalidated.

Tried Solution:
Now, when setting session time out programatically, I am creating variable in my custom session class to define session timeout and session timeout left.

I am adding AbstractAjaxTimerBehavior in my application form class. This is triggering every 5 sec. In  onBeforeRender() method of my form, i am resetting the timer to intial value which means that when form is reloaded, it resets counter for session time out to initial value.

Now here the chalange is coming with ajax calls. Whenever any ajax call is made, it is not reloading that form/component and thus, I am not able to reset the session time out.

Is there any way that this problem can handled.

Below is the form that contains timer and ajax button..
==============================
public class AutoSavedForm extends Form {
        public static final int TIMERINTERVALMINUTES = 5;
        private static final long serialVersionUID = 1L;
        private String textarea;


        public AutoSavedForm(String id)
        {
                super(id);
                TextArea textAreaInput = new TextArea("mytextarea", new PropertyModel(this, textarea));
                add(textAreaInput);
               
                final IndicatingAjaxButton button=new IndicatingAjaxButton( "button",this){

                        /**
                         *
                         */
                        private static final long serialVersionUID = 1L;

                        @Override
                        protected void onSubmit(AjaxRequestTarget target, Form form){

                                System.out.println("Text from the button : "+ textarea);
                                target.addComponent(this);
                                this.setEnabled(false);
                        }
                       
                };
               
                button.add(new AjaxFormComponentUpdatingBehavior("onkeyup")
                {
                       
                        @Override
                        protected void onUpdate(AjaxRequestTarget target) {
                                target.addComponent(button);
                                button.setEnabled(true);
                               
                        }
                });
                add(button);

                add(new AbstractAjaxTimerBehavior(Duration.seconds(TIMERINTERVALMINUTES))
                {
                        private static final long serialVersionUID = 1L;

                        @Override
                        protected void onTimer(AjaxRequestTarget target)
                        {
                               
                                if (!AutoSavedForm.this.isVisible())            
                                {  
                                        // If we don't check this, it will decrement for every page   // in the cache.            
                                        return;    
                                }
                                MySession.getMySession().setSecondsToTimeout(MySession.getMySession().getSecondsToTimeout()-TIMERINTERVALMINUTES);  
                        }      
                });
        }
       
        public void saveContent()
        {
                System.out.println("Text to save  :" + textarea);
        }
        protected void onBeforeRender()
        {
                super.onBeforeRender();
                int timeOutValue = 60;
                System.out.println("Inside on before render....... Here we need to reset session time out value");
                MySession.getMySession().setSecondsToTimeout(timeOutValue);
        }
}
================================