Weird session problems..
Hi,
I have been struggling with this problem for a while, please any suggestions.
I want to check if a user is logged in (thus a session exists with user's username) and if it is to allow him to view the page otherwhise to prompted him to the login page.
So I have this code:
String username=(String) session.getAttribute("username");
to get the username from session. If I ONLY have this line, when the user is logged in he can view the page fine, otherwise I get errors about passing a null field, which is understandable since session does not exist.
So I try to add:
if(username==null || username==""){
%><jsp:forward page="login.html" /> <%
}
%>
and then even if the user is loggen is, I still get forwarded to login.html
I even tried with
response.sendRedirect(response.encodeRedirectURL("login.html"));
but then it just bypasses that if statement and goes through rest pf code, again giving me the null exception.
any ideas?
thank you
[1280 byte] By [
despinaa] at [2007-11-14]

Username is passed correctly because as I said when I dont have that piece of code where I check if it is null the username is printed correctly.
is set to session with
session.setAttribute("username", username)
but if it is unset then it is null right? so I should only get forwarded only when at null.. but somehow it doesnt work
> Why would you, ever during the connection, remove the
> username from session ? Maybe you remove it somewhere
> before the test ?
Im not sure I understand the question, but before the user signs it, there is no session with user's username in it, therefore null. Once the iser signs in, the session is set with user's username. The user can then navigate to all other pages that require session but not in this particular one, that is why this is so confusing to me. What is more weird is that it sometimes work and sometimes not. sometimes it will allow the user to view page and other times it will just prompted him to login.html
do you see anything that might cause this kind of problem?
Still doesnt work I'm afraid.. any other ideas?
the problem is with this line:
username==null
I cannot use the .equals(null) because I get null pointer exception
basically if I have it as .equals(null) it works when user is logged in but when not, I get the null pointer exception
Message was edited by:
despina