servlet request + javax Filters: how do I redirect users to another page? | Community
Skip to main content
jayv25585659
Level 8
September 25, 2025
Solved

servlet request + javax Filters: how do I redirect users to another page?

  • September 25, 2025
  • 1 reply
  • 222 views

might be best explain with an example:

 

  • I have a servlet that returns true or false if HTTP post is successful.
  • I also have a javax filter that checks if a user still has a session. if no session, redirect the user to the login page.
  • User has 2 browser tabs open and both of them are displaying a form (could be the same or different forms).
  • in tab1, user logs out.
  • in tab2, user decides to submit the form (which is calling an servlet).
  • In my filter and when processing the request for tab2, I w ant to redirect the user to the login page since they already logged out in tab1.

How can I do this? I have tried response.sendRedirect and request.getRequestDispatcher. Both of them does not redirect the user to another page.

 

Thanks

Best answer by Karishma_begumSh

Hi @jayv25585659 ,

 

sendRedirect doesn’t work for AJAX/form posts because the browser won’t auto-navigate.

In your filter, detect missing session and send a 401/440 response.If no session

In your frontend JS, catch that status and explicitly

window.location.href="/login";

That way, when tab2 submits after logout in tab1, the user is redirected to the login page.

 

Hope this helpful 🙂

 

Regards,

Karishma.

 

 

1 reply

Karishma_begumSh
Karishma_begumShAccepted solution
Level 4
September 25, 2025

Hi @jayv25585659 ,

 

sendRedirect doesn’t work for AJAX/form posts because the browser won’t auto-navigate.

In your filter, detect missing session and send a 401/440 response.If no session

In your frontend JS, catch that status and explicitly

window.location.href="/login";

That way, when tab2 submits after logout in tab1, the user is redirected to the login page.

 

Hope this helpful 🙂

 

Regards,

Karishma.