Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to validate session in jsp  RSS feed

 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to validate session in all jsp pages.If session is expired i need to redirect to some other page.In what way should i validate session.Should i use a class or common jsp page to validate session?.It will be much helpful to me if anybody gives code for validating session.
Thanks in advance.
Regards,
Vasu
 
S Chandra Mohan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vasu
Its better that u validate the session using a classfile.
Create a classfile which will take the request and response of the jsp page as parameters.
Use request to check the session and response to direct to the page if session expires.
This will work out splendidly
 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If u can give me the sample code it will be great
 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getSession() should return the session object. Even implicit session object is available by default in all jsp. So check whether the object is null or not? If it is null, then redirect to login page else continue.
 
S Chandra Mohan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
the sample code can be like
Lets say u want to fetch userid from the session.
The code might be like.
************
String getUserid(HttpServletRequest request, HttpServleResponse response)
{
HttpSession session = request.getSession(true);
String userid = (String)session.getValue("userid");
if (userid == null)
response.sendRedirect("Timeout.html");
return userid;
}
*************
Above is just a crude idea and u can develop from this.
Hope u have some basic knowledge regarding sessions.
 
Ashish Pagare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also cal isNew() method on the session object to find out if it newly created session.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!