• Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternative to checking getAttribute for null in JSP  RSS feed

 
Jeremy Medford
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
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: 66307
    152
    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...
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!