• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

using jsp:useBean tag

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic