aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why is EL invalid in this example? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why is EL invalid in this example?" Watch "Why is EL invalid in this example?" New topic
Author

Why is EL invalid in this example?

Dishi Jain
Ranch Hand

Joined: Jan 16, 2011
Posts: 46

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
Bartender

Joined: Mar 28, 2008
Posts: 1234
    
  67

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

Joined: Jan 16, 2011
Posts: 46

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
Bartender

Joined: Mar 28, 2008
Posts: 1234
    
  67

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

Joined: Jan 16, 2011
Posts: 46

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
Bartender

Joined: Mar 28, 2008
Posts: 1234
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is EL invalid in this example?