wall-nut
wall-nut

Reputation: 403

JSP Session Variable Unavailable

I am a Perl developer working on JSP pages. All is going well except for one final problem. I am getting the following error when I refresh a page where the session has expired.

org.apache.jasper.JasperException: An exception occurred processing JSP page... at line 10

On line 10 the code pulls the session variable which no longer exists. Is there a way to write the code so it stores a false value if the session variable does not exist.

boolean b_checkin = ((Boolean)session.getAttribute("session_checkin"));

Thanks for your help.

Upvotes: 0

Views: 76

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Try using Boolean instead. boolean is a primitive type and can only hold true or false values. On the other hand, Boolean is a class wrapper for the boolean primitive type, since it holds an object it accepts null value too. So, you could update your code to:

Boolean b_checkin = (Boolean)session.getAttribute("session_checkin");
//if the session expired, then define a default value (probably false)
//otherwise, use the value obtained from session
b_checkin = (b_checkin == null) ? false : b_checkin;

A short-hand way to do it:

Boolean b_checkin = Boolean.TRUE.equals(session.getAttribute("session_checkin"));

Upvotes: 1

Justin Paul Paño
Justin Paul Paño

Reputation: 936

Try this:

boolean b_checkin = session.getAttribute("session_checkin")!=null;

If the *session_checkin* attribute is null then b_checkin is false. Otherwise true.

Upvotes: 1

Related Questions