aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:useBean standard action" Watch "jsp:useBean standard action" New topic
Author

jsp:useBean standard action

Ryan McClain
Ranch Hand

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

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

Joined: Jan 10, 2002
Posts: 60046
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

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

Joined: Sep 09, 2013
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:useBean standard action
 
Similar Threads
jsp:usebean
bean-related standard actions exercise
Question from HF second edition
HFSJ 2nd edition page 420 - Be the Container Answers
HFSJ Question Pg No 356