This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using scriptlets and JSTL

 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Am facing a strange problem. i am checking for a session attribute in my jsp. When i use scriptlets it works fine but when i try to use JSTL it dosent seems to work out.

This gives me-: Not present with scriptlets

while when i use this-:

Gives me nothing

Any ideas???
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should be in the JSP forum, since it's not Struts-related.

Why is only half of your expression in the JSTL expression?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Firstly, you should use proper indention(opening bracket, closing brackets) which helps us to understand the problem code better.

Second, your syntax for EL in JSTL expression is wrong,

EL evaluates the attributes stored in session scope and print it, if it unable to find it in the scope, it simply return empty string, that's mean it prints nothing. So you can able to find out your problem now and Google about "How to check null objects using EL" for workaround.

HTH
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar/David,

Thanks for the showing me the correct way. Its fine now (added that empty operator ). Regarding code am sorry as i just wrote it like draft to check how it goes. Will keep that clear next time.

Thanks a lot again..
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Sagar: That's not correct: if there's a value in sessionScope then ${sessionScope.value == null} works fine. The OP's off-topic code doesn't work because the EL expression is terminated before the null check.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

I tried to get output of sessionScope.user but it was giving me nothing. I guess it returns empty only if attribute is null.
As gave me nothing not even null... but checking with that empty operator worked out fine...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just ${sessionScope} is meaningless; you'd need to specify an attribute.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops Sorry I meant this:

This wont fetch any value is "user" is null. It results as Attribute is-: on browser
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@Sagar: That's not correct: if there's a value in sessionScope then ${sessionScope.value == null} works fine. The OP's off-topic code doesn't work because the EL expression is terminated before the null check.

hmm, after giving some deep thought, I found some points to share.
  • ${sessionScope.value}, returns an empty string "", if 'user' is not in session scope.
  • ${sessionScope.value == null}, returns an Boolean value
  • c:if test=${sessionScope.value} == null, This is something interesting, because "test" attribute takes only EL expression which results into a 'boolean' value OR 'true'/'false' string value. So in this example, if this string is considered as EL expression, then we know that its not an EL and failed to evaluate and returns false(I assumed this because of the behavior, I don't know the actual implementation). So what David says, " EL expression is terminated before the null check." is related to this(Just correct me here).
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic