aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes A question about standard action jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "A question about standard action jsp:useBean " Watch "A question about standard action jsp:useBean " New topic
Author

A question about standard action jsp:useBean

Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
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?


"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
OCPJP 6, OCEJWCD 6
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61220
    
  66

What happened when you tried it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
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

Joined: Jan 10, 2002
Posts: 61220
    
  66

Is there something that would make you think that it would not work as expected?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61220
    
  66

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

Joined: Nov 14, 2012
Posts: 93
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about standard action jsp:useBean