• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalidating a valid session in jsp

 
shalaka wadekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,


I have got a task to perform in my jsp page. My jsp page gets called on click of a link in my application. What I need to do is My jsp page should check if the current session is still valid & if it is then invalidate the session & redirect to another page outside the application. I know how to do the redirection part.
Please let me know how to check, if the sesion is valid & invaidate it if it is valid.

Thanks in advance,
Shalaka
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will help,



 
Naga Niranjan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check whether session object is null or not. If the session object null means, session is already expired or not yet initiated. Use invalidate() method on session object to invalidate a session if the session object is not null.
 
shalaka wadekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks buddy!!

Will check this out
 
shalaka wadekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I tried out the code. So it looks like below. After clicking the link that invokes my jsp page, it redirects me to the given url... But it doesnt end my session in the application. What do I do? Any idea?


 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want you to try this :


I don't know whats the difference between JSP paged predefined "session" object and the above explicitly taken "session" object from request.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shalaka wadekar wrote:Hi,

But it doesnt end my session in the application. What do I do? Any idea?



How you come to know? and also discourage scriptlet ,invalidate session in servlet
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my knowledge in a jsp page session object can never be null, because if you look at the _jspService method of servlet(jsp compiled servlet) the following line of code gets executed session = pageContext.getSession(); means your session object is not null. So as per me one solution to your scenario code be put some value in session object in some page for e.g.

page1.jsp
------------

session.setAttribute("something","somevalue");

now in your executing jsp page apply a check on the attribute

<%
if(session.getAttribute("something",)!=null) // session has not been invalidated yet or it still exists
{
session.invalidate();
}
%>
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:As per my knowledge in a jsp page session object can never be null, because if you look at the _jspService method of servlet(jsp compiled servlet) the following line of code gets executed session = pageContext.getSession(); means your session object is not null.


yes,you are right .

so what is the problem with

?
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(null != session) , the condition will never return true, because the session object itself is never null, what I have done is compared null with an attribute stored in session.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:if(null != session) , the condition will never return true, because the session object itself is never null


please, see the condition carefully . did you test it?
 
pranjal mishra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:As per my knowledge in a jsp page session object can never be null, because if you look at the _jspService method of servlet(jsp compiled servlet) the following line of code gets executed session = pageContext.getSession(); means your session object is not null. So as per me one solution to your scenario code be put some value in session object in some page for e.g.

page1.jsp
------------


set a value into the session at the call of first servlet or action
suppose you're login into an application then the servlet which you are using for the validation part of the login, set a value into the session like


then on the servlet you're redirecting to the another page use this code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic