aspose file tools*
The moose likes JSP and the fly likes Invalidating a valid session in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Invalidating a valid session in jsp" Watch "Invalidating a valid session in jsp" New topic
Author

Invalidating a valid session in jsp

shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

This will help,





[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
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

Joined: Mar 21, 2008
Posts: 37


Thanks buddy!!

Will check this out
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
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

Joined: Feb 19, 2008
Posts: 2902
    
    1


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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 08, 2009
Posts: 1478
    
    6

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();
}
%>


Swastik
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 08, 2009
Posts: 1478
    
    6

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 08, 2009
Posts: 21
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


SCJP 1.5(95%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalidating a valid session in jsp