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
posted
0
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?
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: 488
posted
0
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
posted
0
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
posted
0
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.