Session Validation in Ajax
Session validation in servlet:
Usually if it is a servlet we can simply check
if(session != null ){ // Business code }else{ // Redirection code }
Session validation in ajax:
If you call the servlet from ajax then the above steps won’t work, since ajax gets everything as response, so the code which you have written in the else block (Redirection code) will also be sent as response, so expected redirection will not happen.
So what can we do ?
We have to send the response from servlet to ajax (eg: json ) for both the cases (session valid and session invalid).
if(session !=null) { json.put("session_status",true) }else{ json.put("session_status",false) } out.println(json.toString());
we have to check the session_status in ajax whether it is true or false like below,
if(value.session_status){ // value is the variable in ajax which will receive the response from servlet // It will be varied in your cases. // Business code }else{ redirection(); // redirection is the function name. }
You can redirect by window.open or window.location,
Function redirection(){ window.open("http://www.ngdeveloper.com"); }
If you want to know more about opening new window please read this Post
Thanks for reading this post…………..!!!