• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean standard action

 
Ryan McClain
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
referring to: "Head First Servlets & JSP", 2nd edition
page 420, chapter 8, "Be the container" solution

Book snippet:

----------------------------------------------------------------------------------------------

abstract class Person
String getName()
setName(String)

concrete class Employee
int getEmpID()
void setEmpID()

Look at this standard action:

Question 1: What happens if the servlet code looks like:

Answer:
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.

----------------------------------------------------------------------------------------------

Problem:
How can that JSP code fail at request time, if the servlet sets the attribute "person" in the request scope? Isn't foo.Employee the type of the attribute"person"? Or is it foo.Person? I'm confused.
 
Ryan McClain
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the authors meant that <jsp:setProperty> can't be used because the bean already exists at request scope. I could be wrong. (Why would the request fail if <jsp:setProperty> can't run?). Still looking for answers.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it out to verify that it actually does fail?

It's been over a decade since I've used obsoleted actions like setProperty so I don't remember the nuances.
 
Stefan Evans
Bartender
Posts: 1755
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:setProperty> tags nested within useBean tags only evaluate when the bean is created.

In this case it specifies a "type" attribute without a "class" attribute.
That means that this setProperty tag would never actually run because you can never create the bean, only use an existing one.

So if it does actually execute, I expect it to print "Evan" as the name, and not "Fred"
 
Tony Huang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another post says it's an error of the book
http://www.coderanch.com/t/506641/java-Web-Component-SCWCD/certification/jsp-useBean-class-attribute

I tried to write a servlet and jsp file as specified and it worked well
the page prints:
Name is: Evan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic