Session Expired

 

Session ExpiredIn Web Application development, many times we come across a situation where we need to check if our session is expired or not.

 

What is session expired?

 

Session_Expired is a server-side event, meaning it is triggered on the web server and has nothing to do with a request by the client.

 

1.       On each client request, check if a specific Session variable is set. If it is not, it means the previous Session has expired and the new Session must be populated.

 

2.       Have a javascript call on the client that periodically goes back to the server to check if the Session is still valid. If the Session has expired, you can warn the user that their Session is about to expire.

 

3.       On each client request, we can check if our session is valid or not.

 

In my demo I am going with third step where on each client request I am calling a method to check that if my session is still available or not.

I have a SAP Gateway response were we get a popup to enter our system username and password for validation.

Session Expired

SAP Gateway server asks to login again if we have crossed some time limit that means our session has been expired.

In many web based developments we force our users to login again to the website if they are victim of session expired event. ????

I have created a simple SAPUI5 application. This is the structure. For demo purpose I have created three html files for navigation. You can do it with views also.

Session Expired

Now this is the code in my Index.html.

 

                                                                                                                          

</html

>

Now this is the code in my Index2.html. Simply I have kept buttons in both index2.html and index3.html file.

 

                                                                                                                          

 

Now this is the code in my Index3.html.

 

                                                                                                                          

 

This is my view’s code.

 

 sap.ui.jsview("sessionexpired.view1", {        /** Specifies the Controller belonging to this View.        * In the case that it is not implemented, or that "null" is returned, this View does not have a Controller.        * @memberOf sessionexpired.view1        */        getControllerName : function() {               return "sessionexpired.view1";        },        /** Is initially called once after the Controller has been instantiated. It is the place where the UI is constructed.        * Since the Controller is given to this method, its event handlers can be attached right away.        * @memberOf sessionexpired.view1        */        createContent : function(oController) {                         var oButton = new sap.m.Button({                      text: "Check Session Expired",                      press: oController.checkForSession               });               return oButton;        } });

This is my controller’s code.

 sap.ui.controller("sessionexpired.view1", {        checkForSession : function(){ var str="chksession=true"; jQuery.ajax({                 type: "GET",                 url: "proxy/http/HOST:PORT/sap/opu/odata/sap/ZDC_TEST_SRV/",                 data: str,                 cache: false,                 success: function(res){                     if(res == "1") {                     alert('Your session has been expired!');                     window.location.replace("index2.html");                     }else{                     alert('Your session has not been expired!');                     window.location.replace("index3.html");                     }                 } }); } });

References:-

https://stackoverflow.com/questions/12502295/redirect-to-login-page-after-session-timeout

https://w3lessons.info/2014/01/01/how-to-check-expired-sessions-using-php-jquery-ajax/

 

I generally use simple codes for better understanding. You may always go with complicated ones.

Regards

Dhananjay

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !