Session Problem

I'm using a session to determine whether a user is allowed to view the page or not.

In logincheck.jsp:

HttpSession session1 = request.getSession();

session.setAttribute("gegevens", gegevens);

'Gegevens' is an array containing some important information, such as user id and role.

My application works, using the following.

request.getSession(true);

gegevens = (int[])session.getAttribute("gegevens");

int bedrijfid = gegevens[1];

int level = gegevens[0];

I get all the information I want, but it obviously throws an error when there is no session. That's what it's supposed to do, since it can't find the 'gegevens'-array I'm requesting when it has never been put into the session.

But, how can I determine whether there is a session or not? I tried multiple things, such as using a try/catch and let it throw a NullPointerException, use if/else-statements to check whether the session object is empty or not, but it just doesn't work.

I just tried it with if/else-statements, as the following.

if(!session.equals("")){

out.println(session);

}else{

out.println(session);

}

But it prints the following:

org.apache.catalina.session.StandardSessionFacade@963b33

That's on a sessiontestpage, but when I go to a 'real' and secured page, it throws a NPE for not finding the variables I declared on top of the page.

Anyone any idea?

[1849 byte] By [Nidhuggura] at [2007-11-27 7:55:52]
# 1

try this:

if(session.getAttribute("gegevens")!=null)

{

gegevens = (int[])session.getAttribute("gegevens");

int bedrijfid = gegevens[1];

int level = gegevens[0];

}

JavaPaladina at 2007-7-12 19:37:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Works perfectly!Thanks for the solution.
Nidhuggura at 2007-7-12 19:37:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Welcome m8 :)
JavaPaladina at 2007-7-12 19:37:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...