wood burning stoves 2.0*
The moose likes JSP and the fly likes null in JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "null in JSTL" Watch "null in JSTL" New topic
Author

null in JSTL

Tina Desai
Ranch Hand

Joined: Mar 13, 2003
Posts: 365
Hi All,

I am new to JSTL and need some help on the values we put in different scopes.

I have kept some values in session scope and am planning to write the tag to use sessionScope and then display some conditional content inside the tag.

In a scriplet ideally I would first test of the value is null or not, if not then I will write the comparison.
Like

I read that the scope variables like sessionScope are implemented as Map in JSTL. I do not know which implementation though, so do not know if it allows null or not.

All I need to do is avoid any failure due to null in my JSP.
Do we test null as -


I have not seen such checks in any of the examples I read so I am a bit confused as to what is the standard way of doing these null checks, or are they handled in the JSTL by default?

Thanks in advance.

Regards,
Tina


Alongwith being a good coder, try to be a good professional as well!
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
All I need to do is avoid any failure due to null in my JSP.
Do we test null as -

Easiest way is the EL 'empty' operator:

This checks if the variable is null, an empty string, an empty array or collection etc. and is pretty useful. It will certainly catch the case you are looking for.

Note however that if you are using EL, it suppresses almost all occurrences of null anyway - you'll rarely (if ever) see a NullPointerException. This can be useful when you want to suppress NPEs but don't want to have to write explicit exception handling code!

With JSTL you would need to take account of the possibilities for nulls as the tags don't suppress NPEs like EL does. So you would either have to use the 'empty' operator above to check for nulls up-front, or alternatively use <c:catch> to handle the exceptions yourself.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null in JSTL