Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is EL invalid in this example?

 
Dishi Jain
Ranch Hand
Posts: 46
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create a JSP that generates some JavaScript code to populate
an array of strings used on the client-side. Which JSP code snippet will create this array?

[code=java]
A. MY_ARRAY = new Array();
<% for ( int i = 0; i < serverArray.length; i++ ) {
MY_ARRAY[<%= i %>] = '<%= serverArray[i] %>';
} %>
B. MY_ARRAY = new Array();
<% for ( int i = 0; i < serverArray.length; i++ ) {
MY_ARRAY[${i}] = '${serverArray[i]}';
} %>
C. MY_ARRAY = new Array();
<% for ( int i = 0; i < serverArray.length; i++ ) { %>
MY_ARRAY[<%= i %>] = '<%= serverArray[i] %>';
<% } %>
D. MY_ARRAY = new Array();
<% for ( int i = 0; i < serverArray.length; i++ ) { %>
MY_ARRAY[${i}] = '${serverArray[i]}';
<% } %>

[/code]

Answer: C

I understand that A and B are not correct, but can someone please clarify why is D incorrect?

Urgent.
Thanks
Dishi
 
Tim Cooke
Sheriff
Pie
Posts: 3078
127
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dishi Jain wrote:Urgent.

Only for you. Not for anyone else here. You should read this --> EaseUp

Why don't you explain to us what it is about option A and B that makes you believe them to be incorrect.
Then explain what it is about C that leads you to believe it is correct.
Then have a look at D at tell us what you see. What is it about D that makes you unsure whether it's incorrect?
 
Dishi Jain
Ranch Hand
Posts: 46
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Tim,

Seems you took it so hard on yourself

Ok, so to answer your questions,

A - incorrect - scriptlets cannot have expressions inside them
B - incorrect - scriptlets cannot have ELs inside them

C is correct as per the other forums. And yes it is. Actually while answering your questions i noticed that D is an assignment task, without any scriptlets..I noticed the ';'

Sorry for the inconvenience caused..
However I would still want you to confirm that my assumption is correct

PS-I wrote urgent because I have my ocwcd on coming Monday!!

Thanks,
Dishi
 
Tim Cooke
Sheriff
Pie
Posts: 3078
127
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say the reason option D is incorrect is because of a scoping issue between Scriptlets and EL. The variables i and serverArray[] are defined in a Scriptlet, but then access is attempted through EL, which won't work because scriptlet scope is not the same as EL scope. If you tried it you would probably just get an empty string for that placeholder in your JavaScript presentation.
 
Dishi Jain
Ranch Hand
Posts: 46
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes..I dint think about scopes at all. It can be seen that way too, makes it more clear for me.
I appreciate your inputs.

Thanks
 
Tim Cooke
Sheriff
Pie
Posts: 3078
127
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
Best of luck with your exam on Monday. I'm sure we'll see you back in the Certification Results forum hopefully with good news.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic