Session Validation in Ajax

Session validation in servlet:

Usually if it is a servlet we can simply check

if(session != null ){
// Business code
// 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) {

we have to check the session_status in ajax whether it is true or false like below,

// value is the variable in ajax which will receive the response from servlet
// It will be varied in your cases.

// Business code
redirection(); // redirection is the function name.


You can redirect by or window.location,

Function redirection(){"");

If you want to know more about opening new window please read this Post


Thanks for reading this post…………..!!!

446 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *