Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null in JSTL

 
Tina Desai
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic