• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Head First Servlets and JSP Chapter 8 Mock exam

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

2 questions :

In chapter 8's mock exam, there are 2 questions for which I didnt quite get the answers...I was wondering if it is a typo else can someone please explain how it works..

Question 6:

<%java.util.List list = new java.util.ArrayList();
list.add("a");
list.add("2");
list.add("c");
request.setAttribute("list", list);
request.setAttribute("listIdx","1");
%>
<%insert code here%>

which inserted lines below are valid and evaluate to c

possible answers:
a. ${list.2}
b. ${list[2]}
c. ${list.listIdx+1}
d. ${list[listIdx+1]}
e. ${list['listIdx'+1]}
f. ${list[list['listIdx']]}


I understand how
b.${list[2]} and d.${lost[lisId+1]} can be answers to this question...
but not how f ${list[list['listIdx']]} can be a possible answer

does this indicate that 'listIdx' in ${list[list['listIdx']]} is transformed to listIdx and the attribute list is searched for listIdx?



Also question 17.

Which about the EL access operators are true.

C. If the . dot operator is used to access a bean property but the property doesnt exist, then a runtime exception is thrown.

This has been marked as true but it is mentioned in the book that EL expressions handle nulls well and will not throw an exception.

So is C also a valid choice ?


Also, where can I find the errata for the Head First Servlets and JSP

Thanks,
Roger
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 6:

You're right, the single quotes must be removed from option F or else this will cause an Exception.

Question 17:

If you use ${myVar} and myVar is null, nothing will be printed. But, if myVar is a non-null bean and does not have a property called myProp, the following will cause an Exception: ${myVar.myProp}

C is correct.
[ September 14, 2004: Message edited by: Anthony Watson ]
 
Roger Federer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anthony for the reply.

But on page 395 of the book, it is mentioned that

EL handles null values gracefully...
...
Assume that there is not an attribute named "foo" but there is an attribute named "bar" but that bar does not have a property or key named "foo".

${foo}
${foo[bar]}
${bar[foo]}
${foo.bar}

works fine..and nothing prints for these 4.
Isn't ur example the same as ${bar[foo]}.

Bar exists but foo does not. So ideally, it should not throw an exception coz foo is not a key or property in bar.

What you are saying is similar to ${bar[foo]}. ${bar.foo} is the same as ${bar[foo]. Correct ?

"If you use ${myVar} and myVar is null, nothing will be printed. But, if myVar is a non-null bean and does not have a property called myProp, the following will cause an Exception: ${myVar.myProp}"
 
Roger Federer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I think I got the answer.

${bar["foo"]} is the same as ${bar.foo}
and
${bar[foo]} is not the same as ${bar.foo}

The authors are talking about ${bar[foo]} where foo does not exist as an attribute.

Right ?
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I option F for Q6 works legally. Its ok to have either single or double quotes.
 
Roger Federer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that both single and double quotes are allowed in EL expressions.
but ${list[list["listIdx"]]} or ${list[list['listIdx']]} would not work because the arraylist does not have an index "listIdx".

If "listIdx" is changed to listIdx, then the request attributes are looked at to see if there is listIdx and if there is one (as is the case here), it is replaced with the value from the request attribute.

I hope this is clear.
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger,

EL DOES handle nulls gracefully. So, if you use an attribute that does not exist in any scope in EL, nothing gets printed. However, if you have a bean that exists and you try to access a property that doesn't exist, the problem is that the JSP is trying to call a method that does not exist to access the property, which causes an Exception. If your attribute is a map that does exist and not a bean, you can try to access non-existent keys all day and there won't be any Exceptions thrown. Here are some examples:
 
Roger Federer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Toni for the response. That makes it real clear.
 
author
Posts: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roger,

I would have thought that with all your tennis winnings you wouldn't need to fall back on Java for a second income

We think it's friendlier around here if people use their own name, and apart from being nice, it's really our only rule

Thanks,

Bert
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic