• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about standard action jsp:useBean

 
Joe Allen
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:


Given servlet code like this:



This servlet forwards the request to the following JSP code:



My question: would it work, and what is the result?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Joe Allen
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not tried it yet. I have not set up my environments yet. In fact, I changed the original question in HFSJ page 420, trying to confirm that my change would work, as I think it would. Thanks for prompt reply.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there something that would make you think that it would not work as expected?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I know that you are studying for the exam, but be aware that JSP actions such as getProperty and setProperty are obsolete, replaced by the JSTL and EL as of JSP2 in 2002, so they're not used in modern JSP code anymore (or at least shouldn't be).
 
Joe Allen
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks very much for the advise. Yes, I am studying for the exam, and I am just right at EL chapter of HFSJ and see how simple EL is.
As for the question above, the original question (HFSJ p420) is :

<jsp:useBean id="person" type="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>
Name is: <jsp:getProperty name="person" property="name" />

What happens if the servlet code looks like:

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

The book answer is:

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.

I changed "person" to "employee", which is an existing bean attribute. Even if only type(employee) is specified without class, but the Container would find the "employee" class at the request. So I'd think it'd work. Not 100% sure though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic