Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion with using JSP expressions  RSS feed

 
David Coulthard
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have a person object that contains information about a person {name, address, state, zip} and that maps to a database.

I tried to display it in a JSP; One way works but the other way does not.

Using JSP Scriptlets , It works:


Using JSP Expressions , It does not :


when I use expressions I get this error:

Incompatible conditional operand types String and void


So, does this mean that we can use Expressions only when it evaluates to a value and it can never be used to make null checks ? Am I missing something here or is that an accurate statement ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The modern approach:
which will evaluate to the property value if it's not null, or nothing if it is null. (There's no need for the superfluous space character.)

Assuming that usr gets changed to a modern scoped variable rather than a scripting variable. Java scriptlets in JSP pages have been discredited since 2002. Time to update your approach to JSPs.

P.S. "usr"? Is it really worth the 1-character savings to not opt for clarity and just use user?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to answer your original question, you cannot mix expressions and statements in an expression scriptlet. So, it'd be something along the lines of:

But again, this approach is antiquated, obsolete, discredited and considered a very poor practice at this stage.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.P.S. The use of out.print() in scriptlets to begin with is a poor practice inside of a poor practice. The whole point of a JSP is to allow template text for output. Nesting the template text inside strings, inside scriptlets, is rather inside-out.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:P.S. "usr"? Is it really worth the 1-character savings to not opt for clarity and just use user?

Compln, compln, compln.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:But to answer your original question, you cannot mix expressions and statements in an expression scriptlet. So, it'd be something along the lines of:

Is it that you can't, or is it that the results of the <%= ... %> evaluation might be illegal because the operands of ?: aren't of the same type (and never would be, since out.println is void)?Because if out.print returned a string wouldn't it work?

It wouldn't be what you *wanted*, of course.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, technically, the two operands of the ternary operator require the same type and out.print() is a void method. My use of the imprecise term "statement" meant a statement that returns no value.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just double-checking :)
 
David Coulthard
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for letting me know.

Yes, I do need to update my knowledge to JSP 2.0
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!