In the Head First Servlets & JSP book, they include the following exercise: (Chapter 8, page 416)
Look at this standard action: <jsp:useBean id="person" type="foo.Employee" > <jsp:setProperty name="person" property="name" value="Fred" /> </jsp:useBean> Name is: <jsp:getProperty name="person" property="name" />
Now imagine that a servlet does some work and then forwards the request to the JSP that has the code above. Figure out what the JSP code above would do for each of the three different servlet code examples.
1.Q) What happens if the servlet code looks like:
foo.Person p = new foo.Employee(); p.setName("Evan"); request.setAttribute("person", p);
1.A) Fails at request time! The "person" attribute is stored at request scope, so the <jsp:useBean> tag won't work since it specifies only a type. The Container KNOWS that if you have only a type specified, there MUST be an existing bean attribute of that name and scope.
2.Q) What happens if the servlet code looks like:
foo.Person p = new foo.Person(); p.setName("Evan"); request.setAttribute("person", p);
2.A) Actually, this servlet fails to compile. foo.Person is now abstract, so we can't instantiate the foo.Person
3.Q) What happens if the servlet code looks like:
foo.Employee p = new foo.Employee(); p.setName("Evan"); request.setAttribute("person", p);
3.A) This works fine and prints out "Evan". Remember, the code INSIDE the body of <jsp:useBean> will never run, since we specified a type without a class.
I don't understand why they all wouldn't fail for the same reason the first one failed. There is no attribute in page scope called "person", since it is always being added to the request. The default scope is page, and since we define a type and not a class in all instances, it should fail for the same reason as the first one ...--->>Right???
You are absolutely right about this! This is one of our known errata in the book... =======================
So, good catch there. You're thinking about it in exactly the right way.
Here's the main errata we have so far (I think there are couple more but I can't get access to the page right now): ========================
1) Question #6 in Chapter 7, the answer should be "D" and not "E".
2) Be the Container in chapter 8, the third example will not work for the same reason that the first example doesn't work--because the attribute is at Request Scope, and the default scope is Page scope.
3) Pg. 114, upside down text should say "Post is considered NOT idempotent".
4) Question 5 in the Filters chapter, the book says the answers are "A" and "G", but "D" should ALSO be marked as a correct answer.