• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in c:if

 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just now i am trying sample programs in JSTL

<%
String name = "vidya";
%>
<c:if test="${name eq 'vidya'}">
${name}
</c:if>

Above program doesnot print anything

But when i change like this, it is working

<%
String name = "vidya";
request.setAttribute("name",name);
%>
<c:if test="${name eq 'vidya'}">
${name}
</c:if>

Why first one is not working???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${name eq 'vidya'} looks for "name" in a scope.
Your first example does not set the "name" variable in any scope.

The second example sets it in the request scope, so it makes it visible to c:if. You could also set it with c:set
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou for your immediate reply.

I have one small doubt??.Suppose if we are declaring some strings in jsp, it wont fall in any scope...is it so???

Suppose in some scenario we want to use that string in <c:if> then we want to set in some scope and retrieve that..is the only way to do so???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is.
You'll have to call either
1. pageContext.setAttribute("name", name );
2. or <c:set var="name" value="<%= name %>"/>

But you should try to avoid using scriptlet in your jsp files anyway

[ May 24, 2006: Message edited by: Satou kurinosuke ]
[ May 24, 2006: Message edited by: Satou kurinosuke ]
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Satou...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic