• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hi, I have a HeadFirst book example question...

 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a question on HeadFirst Servlets & JSP book page 431, question#15. I don't understand how choices C and F cause errors while choices D and E do not. I tried this question out with my own example and I got a blank display for choices C,D,E,F. So I'm wondering if C,D,E,F is the correct answers to the question? Is my own example wrong? I have the original question and my own example pasted below, thanks so much for your great help !

(That "a" variable is not declared anyway, is this intended?)

Thanks,
Carmen.

********** Original question **************

public class MyBean {
private java.util.Map params;
private java.util.List objects;
private String name;
public java.util.Map getParams() { return params; }
public String getName() { return name; }
public java.util.List getObjects() { return objects; }
}

Which will cause errors (assume that an attribute named mybean can be found, and is of type MyBean)? (Choose all that apply.)

A. ${mybean.name}
B. ${mybean["name"]}
C. ${mybean.objects.a}
D. ${mybean["params"].a}
E. ${mybean.params["a"]}
F. ${mybean["objects"].a}

**************** My example *******************
/////////My Bean class:
package foo;
import java.util.*;

public class MyBean {
private java.util.Map params;
private java.util.List objects;
private String name;

public void setName(String name) {
this.name = name;
}

public java.util.Map getParams() {
return params;
}
public String getName() {
return name;
}
public java.util.List getObjects() {
return objects;
}
}

/////////My jsp
<HTML>
<HEAD>
<TITLE>Scriptless JSPs </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>

<jsp:useBean id="mybean" class="foo.MyBean" scope="page">
<jsp:setProperty name="mybean" property="name" value="Freddie"/>
</jsp:useBean>

Hi! Testing mybean.objects.a Mybean is named: ${mybean.objects.a} <!--I just get a blank -->

</BODY>
</HTML>
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the question assumes a map key "a".

And I also think this question is testing on what uses the "." & the "[]" operators can be put into.

mybean.objects=mybean["objects"], both return a list which cannot be used with a "." operator, hence the errors

mybean.params=mybean["params"], both return a map which can be used with a "." operator, hence no errors

I hope my answer is right!
 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ha! Thanks Sue, I've been struggling over this question for a while, so "a" was actually suppose to be part of the question, but the book mistakenly forgot to put it in. Alright, that makes sense then. When the question says "error", do they mean that when ${mybean["objects"].a} returns an error that constitutes an error?

Thanks so much for your response!
Carmen
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic