matthew016
asked on
question about invalidate()
Hi,
A user can, on my website, log out.
1) He clicks on a link
<li><a href="index.html?do=logout ">déconnex ion</a></l i>
The page will be forwarded to the servlet index.html
2) In the servlet, I invalidate the session, then forward to index.jsp
else if(action.equals("logout") ) {
HttpSession session = request.getSession();
session.invalidate();
getServletContext().getReq uestDispat cher(urlIn dex).forwa rd(request ,response) ;
return;
}
3) In index.jsp, I have this code :
<%
metier.IUtilisateur util = (metier.IUtilisateur) session.getAttribute("util isateur");
%>
<% if(util == null) { %>
<link rel="stylesheet" type="text/css" href="css/menu2NonAuthenti fie.css"/>
<% } else { %>
<link rel="stylesheet" type="text/css" href="css/menu2Authentifie .css"/>
<% } %>
And util gives me null, without an exception. (I know the code is not nice)
But why when I try this in a JSP (for testing) :
session.invalidate();
metier.IUtilisateur util = (metier.IUtilisateur) session.getAttribute("util isateur");
if(util == null) {
[...]
I have an error :
java.lang.IllegalStateExce ption: "getAttribute": Session déjà invalidée
org.apache.catalina.sessio n.Standard Session.ge tAttribute (StandardS ession.jav a:1011)
org.apache.catalina.sessio n.Standard SessionFac ade.getAtt ribute(Sta ndardSessi onFacade.j ava:109)
org.apache.jsp.chat2_jsp._ jspService (chat2_jsp .java:61)
org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:97)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 32)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:314)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:264)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
controleur.Controleur.conn ecterChat( Controleur .java:268)
controleur.Controleur.doGe t(Controle ur.java:78 )
controleur.Controleur.doPo st(Control eur.java:3 75)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 709)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
A user can, on my website, log out.
1) He clicks on a link
<li><a href="index.html?do=logout
The page will be forwarded to the servlet index.html
2) In the servlet, I invalidate the session, then forward to index.jsp
else if(action.equals("logout")
HttpSession session = request.getSession();
session.invalidate();
getServletContext().getReq
return;
}
3) In index.jsp, I have this code :
<%
metier.IUtilisateur util = (metier.IUtilisateur) session.getAttribute("util
%>
<% if(util == null) { %>
<link rel="stylesheet" type="text/css" href="css/menu2NonAuthenti
<% } else { %>
<link rel="stylesheet" type="text/css" href="css/menu2Authentifie
<% } %>
And util gives me null, without an exception. (I know the code is not nice)
But why when I try this in a JSP (for testing) :
session.invalidate();
metier.IUtilisateur util = (metier.IUtilisateur) session.getAttribute("util
if(util == null) {
[...]
I have an error :
java.lang.IllegalStateExce
org.apache.catalina.sessio
org.apache.catalina.sessio
org.apache.jsp.chat2_jsp._
org.apache.jasper.runtime.
javax.servlet.http.HttpSer
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
controleur.Controleur.conn
controleur.Controleur.doGe
controleur.Controleur.doPo
javax.servlet.http.HttpSer
javax.servlet.http.HttpSer
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Okay,
One question. For point #3, is "<link rel="stylesheet" type="text/css" href="css/menu2NonAuthenti fie.css"/> " executed?
One question. For point #3, is "<link rel="stylesheet" type="text/css" href="css/menu2NonAuthenti
ASKER
Yes, that all works fine ...
Mm...., this is weird
I just realized that it is not NullPointerException but IllegalStateException. Give me a sec
I just realized that it is not NullPointerException but IllegalStateException. Give me a sec
In point #3,
Do you catch any exception or any error message in the console?
Do you catch any exception or any error message in the console?
ASKER
Nothing.
That's why I post this question, because I don't understand why with 1)2)3) it works
and not when I put the instructions close to each other.
That's why I post this question, because I don't understand why with 1)2)3) it works
and not when I put the instructions close to each other.
I do not really know what happens.
If in my local machine, it definitely throws me the NullPointerException. The reason you get the IllegalStateException is because you use
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html
However, for the working case in the #3, I don't really know why. It should not work. Sorry, can't help further.
David
If in my local machine, it definitely throws me the NullPointerException. The reason you get the IllegalStateException is because you use
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html
However, for the working case in the #3, I don't really know why. It should not work. Sorry, can't help further.
David
ASKER
So do u test each time, before accessing a session, if it is valid ?
Yes,
You have to do that instead of directly accessing it. when the session is valid, it is working fine but if the session is not valid, you will get NullPointerException as you are trying to cast NULL values to certain classes as
metier.IUtilisateur util = (metier.IUtilisateur) session.getAttribute("util isateur");
if it is null, it is somehing like
metier.IUtilisateur util = (metier.IUtilisateur) null;
David
You have to do that instead of directly accessing it. when the session is valid, it is working fine but if the session is not valid, you will get NullPointerException as you are trying to cast NULL values to certain classes as
metier.IUtilisateur util = (metier.IUtilisateur) session.getAttribute("util
if it is null, it is somehing like
metier.IUtilisateur util = (metier.IUtilisateur) null;
David
My suggestion is to keep this thread open for few more days as today is weekend and not all experts are available here.
Hopefully by Monday some other experts may pop in and give you the right answer.
Hopefully by Monday some other experts may pop in and give you the right answer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with suprato45 and jaggernat.
We are talking about two session objects in the 123 testing sequence. But only one session object in the single page test.
We are talking about two session objects in the 123 testing sequence. But only one session object in the single page test.
ASKER
Ok, I think I understand.
Correct me if I'm wrong,
in step two I "delete" the session,
and when I forward to index.jsp (step 3) A new session is created where I do not have attribute "utilisateurs" in it.
Correct me if I'm wrong,
in step two I "delete" the session,
and when I forward to index.jsp (step 3) A new session is created where I do not have attribute "utilisateurs" in it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
agree with rrz
ASKER
Thank u very much for your support !
ASKER
I do exactly the same thing