• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Book Questions

 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 8: Scriptless JSPs

Page 357: I was wondering about this standard action/scripting:

<jsp:setProperty name="person" property="name"
value="<%= request.getParameter("userName") %>" />

Why don't the nested double quotes (") cause a problem? Some sort of parsing error?



Page 356: Question 1 "Be the Container"

What happens if the servlet code looks like this:

foo.Person p = new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

when the jsp standard action is:

<jsp:useBean id="person" type="foo.Employee" > [...]etc.

where foo.Person is abstract and foo.Employee is an extension of Person.


The explanation at the end of the chapter seems to indicate the problem is only with the scope of the "person" attribute. The servlet is apparently storing it at the request scope. The standard action has a page scope by default.
I thought that even if the jsp had scope="request" that it would fail since the real problem is that there is no object of type foo.employee out there. There is a class, but not a type. The type that is known is foo.Person.

Can anyone out there clarify this for me?

Thanks!

The answer to this question says
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that even if the jsp had scope="request" that it would fail since the real problem is that there is no object of type foo.employee out there. There is a class, but not a type. The type that is known is foo.Person.


The reference for the Employee class is super class Person which doesn't have a method to return empID. There is an object of type foo.employee referenced by foo.Person.

Thanks
Chandu
 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Chandra. But I'm still confused here -- I understand that if you do an instanceof test on p for either the Person class or the Employee class it will be true, but I wasn't sure that was what they meant by the "type" parameter.

In any case, by reading the explanation in the book, it seemed not to relate to the "type" of the object but only to its scope. In other words, they seem to be saying the reason it failed was that the jsp reference was in page scope but the servlet was request scope.

Appreciate the input -- still working on understanding it. Thanks...
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the explaination on Pg 416 on the same question I understood that there should be a class for an instance of the object to be created. Without the class the body of <jsp:useBean> is not executed. That should be enough for the code to not compile.

Thanks
Chandu
 
Neo Parker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Regarding your first question about multiple quotes:
There is no parsing required while reading what is typed inside an expression. Whatever is typed between <%= %> is considered as a single expression and evaluated. The value returned by the getParameter goes to the value attribute. So there won't be any error.

Hope this helps.
 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Neo. That's interesting -- I've had so much trouble in the past with nested quotes in so many situations that it just looked wrong! It's nice that we don't hage to worry about that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic