• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Alternative to checking getAttribute for null in JSP  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've recently been told that the following is something to be avoided.

The trouble is, it is possible to arrive at this JSP and have myAttr be null.
Should I look at making myAttr not null?
But what if I don't want to display the contents of myAttr unless it is valid?

Thanks in advance.
 
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never said what part was "to be avoided".

There are a number of things to be said about that code fragment:

  • First and foremost, the very use of scriptlets is over 8 years out of date. At this point, no Java code should ever be placed in a JSP. The JSTL and EL have been available since before the advent of JSP 2.0 in 2002 and the time to put Java code in JSPs is long passed.


  • And yes, if you insist on sticking with scriptlets, then it's possible for the attribute to be null.


  •  
    Jeremy Medford
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for that. Sorry I wasn't specific, but you were right.
    I'm just getting started with JSP and Servlets.
    Funny thing is after I read your response, I saw what should have been completely obvious in my Servlet code...I now ensure that the attribute is never null.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66806
    168
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Jeremy Medford wrote:I'm just getting started with JSP and Servlets.


    Then I would highly recommend starting off on the right foot -- avoid scriptlets and Java code in the JSP. That means anything between <%; %> or <%= %>. The JSTL and EL are the way to go...
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!