File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Head First Servlets and JSP Chapter 8 Mock exam 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 "Head First Servlets and JSP Chapter 8 Mock exam " Watch "Head First Servlets and JSP Chapter 8 Mock exam " New topic
Author

Head First Servlets and JSP Chapter 8 Mock exam

Roger Federer
Greenhorn

Joined: Sep 14, 2004
Posts: 6
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
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
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 ]

Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Roger Federer
Greenhorn

Joined: Sep 14, 2004
Posts: 6
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

Joined: Sep 14, 2004
Posts: 6
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 ?
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I option F for Q6 works legally. Its ok to have either single or double quotes.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Roger Federer
Greenhorn

Joined: Sep 14, 2004
Posts: 6
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

Joined: Sep 25, 2003
Posts: 327
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

Joined: Sep 14, 2004
Posts: 6
Thanks Toni for the response. That makes it real clear.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Servlets and JSP Chapter 8 Mock exam
 
Similar Threads
Doubt in mock question on page no. 433 of Head First Servlets & JSP 2nd edition Chapter 8
Pg. 420 - Q.6 of HF book
EL Coercion question
Please tell me the correct answer
Question on EL