This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes using jsp:useBean tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "using jsp:useBean tag" Watch "using jsp:useBean tag" New topic
Author

using jsp:useBean tag

Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Employee is a simple bean, with name attribute and getter / setter

This is the code in my servlet:

Employee p = new Employee();
p.setName("Evan");
req.setAttribute("person",p);
// forward to a jsp

This is my code in the jsp:

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

According to me and HFJS, this code should work. But somehow it is not working.. Could you tell me why ?

Also,

If I change the jsp tag "usebean" to <jsp:useBean id="person" type="other.Employee" scope="request">

Then the code works fine. I am not able to understand why is this so..
For reference HFJS page no. : 356 has this problem mentioned.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510



Would you please be more specific. Do you mean that you are not getting the results that you expect (if so, what did you expect and what did you get) or it fails completely?
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
javax.servlet.ServletException: bean person not found within scope

java.lang.InstantiationException: bean person not found within scope

this is the exception that i am getting.
sri latha
Greenhorn

Joined: Mar 03, 2006
Posts: 27
person attribute is stored at request scope.if you specify only type you have to specify scope of the attribute.otherwise it searches for the defalut scope("page")and didnt find any attribute so it throws exception. where as if you specify "class" then searches for all the scopes from page to application i think.
Hey see the answer in page 416.you will understand well.
Please correct me if i am wrong.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hi Prasad,

If I change the jsp tag "usebean" to <jsp:useBean id="person" type="other.Employee" scope="request">


Your code works fine when you use the following construct because the bean is by default only searched in Page scope and not in other scopes.
As per your code you are setting the bean in request scope and hence you have to specify the scope as request while using <jsp:useBean>.



regards,
Amit


Regards,
Amit
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
I think you are confusing the concepts of scope with the use of class and/or type.

Scope will define where the bean will be searched for with the default scope set to page. Type is the type of the reference variable that will be created at translation time if the bean is found in the indicated scope. Class is the class of the object that the variable (type) will point to. If your bean cannot be found in the scope which you indicated then an instance will be created if you have indicated the class an a default constructor exists for that class.

Since you did not indicate the class and told the tag to look in the default(page) scope, it could not instantiate a bean when it did not find one.

This is an important concept that is sure to be on the exam.

Try running the code with a class parameter in the useBean tag without indicating a scope and you will see that one is created for you.

I will leave understanding how/why the nested tag is executed to you :-)
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Thanks to all !
The thing is, I had printed all the code as shown in HFJS pg: 356. In that example in the book, the request scope is not mentioned in the jsp:useBean tag, and still they say the example should run fine.

Hence I thought, there is some problem in my understanding; which is not the case.

Its a mistake with the book example, I guess.
 
jQuery in Action, 2nd edition
 
subject: using jsp:useBean tag
 
Similar Threads
Doubt in use bean
Bean related exercise question
jsp:useBean
doubt on jsp:useBean tag
use of type in <jsp:usebean>