I am working with jsp web project now....
In that If some user login with their own login id and password, it shoule be redirected to their own home pages...
i have already done this..
but if some time their own home page is idle, it should be displayed as
"Your session has expired" and it should be redirected to login page...
for this i want jsp code with explanation...
After user gave logout, if user presses "back" button, it shoule not be redirected to their own page... it should be remain still in login page...
for this also i want jsp code with explanation.

Recommended Answers

All 5 Replies

Hi siva,

For this u need to check weather session object (which u created) is null or not if it is null then u need to redirect to login page.

I am giving some example:

this is in server side code ..

HttpSession session=request.getSession(false);
//here u are geeting the user name from session
		String strname=(String) session.getAttribute("username");
//here u are checking the session object (the value which u store in session) if it is null u are redirecting..		
		if(username==null){
			//session=request.getSession();	
				RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
				rd.forward(request, response);
				return;
			}

Like this only u need to check in jsp also.
It can surely redirect to login page..

While u logout use must invalidate the session .
write this code in logout servlet

HttpSession session = request.getSession(false);
		
		
		if(session!=null)
		{
			session.invalidate();
			
		}
		
		RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
		rd.forward(request, response);
				return;

It is better to use filter to check the session object otherwise u need to check the condition in every servlet it become redundancy ..


I hope this is useful for u...

hi siva,

u can use the above code which i post

ya... thanks kalpana... thank you very much...
but after logging out, if i click back button, it again goes to user's home page... but session expiration worked;

HI siva,

U need to check in all JSP pages then only it works...

thanks kalpana it works.......

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.