0

I have this Jsp file

<SELECT name="brandsFrom" onchange="as()">
    <c:forEach items="${brandsSelectedList}" var="brands">
        <c:if test="${brands.name == nam}">
        <option value="${brands.id}">${brands.id}</option>
        </c:if>
    </c:forEach>
</SELECT>

i have declared nam above this code like

<%!
String nam;
%>
<%
nam=request.getParameter("name"); 
out.println("value in the string ="+nam+"+");
%>

When i use

<c:if test="${brands.name == nam}">

the code doesn't rum whereas when i use something like this

<c:if test="${brands.name == 'Denim'}">

the code runs . Why am i not being able to use nam variable ??

Aman Kumar Sinha
  • 407
  • 6
  • 17
  • The check looks correct to me. Are you sure that the values of the variables are what you expect them to be? – f1sh Feb 26 '16 at 10:36
  • Did you get some output from `out.println("value in the string ="+nam+"+");`? – Javasick Feb 26 '16 at 10:39
  • tried `` ? – MaVRoSCy Feb 26 '16 at 12:36
  • 1
    You don't need to use nam. You can use If you insist on using nam, then you should learn the difference between scripting variables and scoped variables. – rickz Feb 27 '16 at 02:40
  • Thanks @rickz your solution worked but i still want to know why the earlier one didn't work . I tried reading about the scope of variables in a jsp file and wasn't able to figure out the reason why it wasn't working – Aman Kumar Sinha Feb 28 '16 at 05:53
  • Look at the answer at http://stackoverflow.com/questions/3570191/i-can-pass-a-variable-from-a-jsp-scriptlet-to-jstl-but-not-from-jstl-to-a-jsp-scriptlet-without-an-error – rickz Feb 28 '16 at 06:13

1 Answers1

0

It could be because brands.name is coming from brandsSelectedList which would be being set in your ActionSerlvet overridden method whereas nam is a local variable defined in JSP. Variables defined in jsp might not be accesible in your expression langauge code. I haven't tried but look at the corresponding generarted class file through a decompiler for JSP and see what's going on there

Anil
  • 1,735
  • 2
  • 20
  • 28