aspose file tools*
The moose likes JSP and the fly likes Using scriptlets and JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using scriptlets and JSTL" Watch "Using scriptlets and JSTL" New topic
Author

Using scriptlets and JSTL

Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 19, 2008
Posts: 2902
    
    1


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

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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

Joined: Sep 29, 2008
Posts: 12617

@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

Joined: Mar 20, 2006
Posts: 284
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

Joined: Sep 29, 2008
Posts: 12617

Just ${sessionScope} is meaningless; you'd need to specify an attribute.
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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).
  •  
    jQuery in Action, 2nd edition
     
    subject: Using scriptlets and JSTL