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???