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]
# 1
Are you sure that the "username" attribute is in the session ? When and how did you put it there ?
abc0xyza at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
Have u tried to print username? what is the output ur getting?
Amit_bhardwaja at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3
Not directly related to the specific problem here, but I would say, for such user authentication actions you'd better to use a Filter.
BalusCa at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

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

despinaa at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
> but if it is unset then it is null right? Why would you, ever during the connection, remove the username from session ? Maybe you remove it somewhere before the test ?
abc0xyza at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6

> 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?

despinaa at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7
try using username.equals(null) username.equals("");
newtona at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8

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

despinaa at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9
Try this String username = ""; if (session.getAttribute("username") != null) {username = String(session.getAttribute);}else{ //forward to error page }Message was edited by: newton
newtona at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 10

if ( (username==null) || (username.trim().equals("")) { %>

<jsp:forward page="login.html" />

<% } %>

tolmanka at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 11
try this ...if (username == null || "".equals(username.trim())){ // do whatever}
knlmhjna at 2007-7-11 > top of java,Enterprise & Remote Computing,Web Tier APIs...