This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using jsp:useBean tag

 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
sri latha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic