wood burning stoves
The moose likes JSP and the fly likes jsp:useBean standard action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:useBean standard action" Watch "jsp:useBean standard action" New topic

jsp:useBean standard action

Ryan McClain
Ranch Hand

Joined: Nov 27, 2010
Posts: 145
referring to: "Head First Servlets & JSP", 2nd edition
page 420, chapter 8, "Be the container" solution

Book snippet:


abstract class Person
String getName()

concrete class Employee
int getEmpID()
void setEmpID()

Look at this standard action:

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

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.


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

Joined: Nov 27, 2010
Posts: 145
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

Joined: Jan 10, 2002
Posts: 63865

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.

[Asking smart questions] [About Bear] [Books by Bear]
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
<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

Joined: Sep 09, 2013
Posts: 2
Another post says it's an error of the book

I tried to write a servlet and jsp file as specified and it worked well
the page prints:
Name is: Evan
I agree. Here's the link: http://aspose.com/file-tools
subject: jsp:useBean standard action
It's not a secret anymore!