when a user logins to my application, he submits a form to be processed through the Servlet. The servlet creates a session for the user. How would I create a link so the user can logout? I cannot seem to directly link to a Servlet. How would I delete the session and link back to the homepage?
Here is a way that I could do it, but it doesn't seem "right". I could link back to the index.jsp?logout=true. My index.jsp will see if logout is true and delete the sessions.
Is there another way to do it?
Write a servlet mapped to /logout
which then executes something like this in doGet
:
HttpSession session = request.getSession(false);
if(session != null)
session.invalidate();
request.getRequestDispatcher("/index.jsp").forward(request,response);
It wont matter if the user has a session or not, they will ultimately be redirected to index.jsp
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With