Hi all,
I'm facing problem while with invalidating session. Actually I want to redirect a user to the login page if the session is invalid either he presses back button or enters a url. Plz help me.

Thanks.

Kindly post your code so we can suggest you better.

I've a code through which I can invalidated the session that I'd created, by pressing logout link provided. The code for session invalidation is as under :

HttpSession ses = request.getSession();
ses.invalidate();

after calling this servlet code my session gets ended and m redirected to login page.
But as a press back button my previous page is displayed with "null" as my username.

I want this would noty happen. I want, when I press back button and as my session got ended simply my login page would be displayed.

How could I do this plz help me.......????

Dear raephel,

You haven't mentioned that you are using Browser's back button Or your application having some back button.
Any way...

Right now let's assume that you are pressing your application's back button not browser's back button.

You can check your session related Objects in head tag of your jsp like this

<html>
<head>
<title>This is page after successfull login</title>
<%
	/** 
          *Get your session related any object here.
          */  
        String name = (String) session.getAttribute("name");
        /** 
          * Check if that object is still there or not.
          * means checking valid session.
          */
	if (name == null || name.trim().length() <= 0) {
        /** 
          * If this object is not present redirect user to login page.
          */ 
		response.sendRedirect("login.jsp");
	}
%>
</head>
<body>
.................
</body>
</html>

And make sure that you remove those session object while you say LogOut.

And code is like this in your servlet...

HttpSession session = request.getSession();
	 session.setAttribute("name", null);
	 session.removeAttribute("name");
	 session.invalidate();
	 response.sendRedirect("login.jsp");

For more clarification I am attaching one sample application. try it and let us know if you want more help on that.

m sorry that I'd not told u about BACK button, actually m using my browser's back button.


As I press logout link, m redirected to login page, but as I press BACK button of my browser I got displayed my previous page whose logout link I'd pressed.

Instead of it I need to get redirected to login page as i press the BACK button of my browser, later on when I enter my login credentials I would be redirected to the same page from where I'd redirected to this login page instead of being redirected to HOME page.

It's li'll bit confusing but plz help me, if u want any clarification plz ask me for the same.

thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.