<jsp:useBean id="person" type="foo.Person" class="foo.Employee"> <jsp:setProperty name="person" property="name" param="userName" /> </jsp:useBean> a request parameter is being set to a property of a new bean attribute in the page scope
I think,if we use body,i.e jsp:setProperty within jsp:useBean,a New Bean is created and the values are set.
Hence if we use jsp:getProperty before setting the values,we will get only Null and not an Exception.The Reason I could give is the bean will created in this case but only the values are not set.Exception will be thrown in the case only when the Bean is not found in the page scope. Hence i feel only Null will be the result.
"a request parameter is being set to a property of a new bean attribute in the page scope."
Am I correct?
This code means, if a bean is not present in the page scope(which is the default, if not specified in useBean), create it in the page scope, and for the bean property "name", call the setName(String) method with the value corresponding to the request parameter name "userName".
<jsp:useBean id="person" type="foo.Person" class="foo.Employee"> <jsp:setProperty name="person" property="name" value="<%=request.getParameter("username") %>/> </jsp:useBean> Does this also create an instance in page scope?
Before explaining any thing I will take the jsp:setProperty as bellow because you have missed one " in the tag. <jsp:setProperty name="person" property="name" value="<%=request.getParameter("username") %>"/>
This is the tag which decides where to create the bean object depending on the scope attribute. Scince we have not mentioned the scope attribute, the default is page scope. So it will create the bean object in page scope.
Using request object in jsp:setProperty will not craete the bean object in request scope. In fact <jsp:setProperty> and <jsp:getProperty> are there to use the object created by <jsp:useBean> Tag.
<jsp:useBean id="person" class="foo.Employee">
Container will create a object like this
foo.Employee person=new foo.Employee();
Then it will add the object into pageContext like bellow.
setProperty is there to set the property(member variable) of a object. Which object? What ever we have mentioned in "name" attribute (name="person").In this case the object is person.
Now the question is setPropery doesn't know where the object persion is.
So it will search for the object "person" starting from PAGE_SCOPE to APPLICATION_SCOPE. If it will find the object in PAGE_SCOPE ,it will stop the searching there( means, it will not go to REQUEST,SESSION and APPLICATION SCOPE).Then it will use that object to set the property.
Hope this will help you.Please let me know if you need more explanation.
Joined: Aug 21, 2005
Thanks for the wonderful explanation Chittaranjan.