Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about JWeb+ mock exam question

 
Sivakumar Jambunathan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In jweb+ mock exams, there is one question on EL which is like

Consider the following code appearing in a JSP file:

<%
request.setAttribute("names", new String[]{ "A", "B", "C" } );
request.setAttribute("index", "1");
%>
<h1><!-- insert code here --></h1>
Which of the following statements will print B in inserted in the above JSP page?
Select 3 correct options

The answer listed are:
b ${names[1]}
c ${names.index}
d ${names[index]}

By the way the other options listed for this question are:
a ${names.1}
e ${names.$index}
f ${names[$index]}


Reading HFSJ, I thought ${name.index}, can be used only when the first attribute is a Map or a Bean and the second one after the . is a key in a Map or a property in a Bean. Further, I remember reading that for Array Lists, List etc the [] operator needs to be used.

Am, I misunderstanding some thing?

Thanks a lot
Siva
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. I tried some coding and proved c is wrong. Here is the error message I got:

The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain to me why option f: ${names[$index]} prints nothing?

Thanks!
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jinghui

The option f is ${names[$index]}
here $index is not in braces {} i guess that is the reason it is not printing
anything

Try ${names[${index}]} I think it will print B again

Catch You Later
Shiva
 
J Johny Rufus
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Will the expression ${names["index"]} will print B ?


Thanks and Regards,
Rufus
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

No , it will not work
This is just plain old java syntax error


${names["index"]}

names["index"] is not a valid java expression since the index of an array
should be integer but here it is string

There is no connection here to EL since there are no ${}
around index
 
J Johny Rufus
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shiva,

Assuming names is a Map

<%
HashMap map = new HashMap();
map.put("1","first");
request.setAttribute("names", map );
request.setAttribute("index", "1");
%>

Will this work ?
${names["index"]}

Tanks in advance
Rufus
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ,


In ${names["index"]} "index" is a string literal so it will not work
However ${names[index]} ie without quotes will work since
we have set the index var in the request

ie ${names[index]} --> ${names["1"]} -------> first

And yeah , i didnt explain properly in my previous post
${names["1"]} is valid in case of EL though not in java
 
J Johny Rufus
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shiva
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shiva,

I tried your suggestion and it didn't work. My code is listed below:



I got this error:


Do you know why?

Thanks!
Jenny
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jinghui ,

You have tried the wrong expression . If you look at my previous post
the expression i had suggested is ${names[index]}
whereas the expression you tried is ${names[${index}]}

This is because when nesting expressions you need to use only one $ sign
If 2 or more present then the error you got is thrown

Morover even with the first expression i dont think it will work
since you are setting the var in the request scope and trying to access it in the same page (which is not in request scope )

try putting it in the page scope with the first expression i suggested

Thanks
Shiva
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Shiva. ${names[index]} works. And I don't have to put it in page scope since the EL automatically checks request scope when it's not in page scope.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic