Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use EL to get the value

 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%
request.setAttribute("vals",new Sring[]{"1","2","3","4"});
request.setAttribute("index","2");
%>
<%-- insert code here --%>
Which three EL expressions inserted at line 15,are valid and evaluate to "3"?(Choose three)

a)${vals.2}
b)${vals["2"]}
c)${vals.index}
d)${vals[index]}
e)${vals}[index]
f)${vals.(vals.index)}
g)${vals[vals[index-1]]}

I have seen this qestion in the mock exam
I have confusion on these types of question.
Could you explain this question clearly.
The implicit objects are param,paramValues,header,headerValues,pageContext,......
But here we are not using any implicit object.Please explain clearly
 
James Mark
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b)${vals["2"]}
evaluates to
third element in the StringArray attribute in the requestScope,which is 3.
Remember indexing start at 0.
d)${vals[index]}
evaluates to
Note that here no singleQuotes or Double Quotes around the index
Inside scriplet there is a statement
request.setAttribute("index","2");
So the expression simplifies to
${vals["2"]} which is again 3

g)${vals[vals[index-1]]}
evaluates to
${vals[vals["2"-1]]}
${vals[vals[1]]}
${vals[2]} which is again 3


EL will serach in all the four scopes in the order from
page,request,session,application.....
Here you put things in the request scope

If you want to be more specific
go for

${requestScope.vals[2]} or
${requestScope.vals['2']} or
${requestScope.vals["2"]}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is more than implicit objects. In EL, you can access scoped variables via their name. When you do so, the variable will be searched in all scopes (page, request, session and application), and returned when found. (actually, the container will call PageContext.findAttribute) In your example, ${vals} will look for a variable called "vals" in the page scope, request scope, session scope and application scope. There is a "vals" attribute in the request scope, the one which has been set in a scriptlet, so ${vals} will return it. Therefore, ${vals} represents an array of String. Note that because "vals" is in the request, you could also access it via ${requestScope.vals}.
 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got it.Thank you Verymuch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic