What does the useBean standard action do when it does not get the instance of bean class?

The usebean standard action either gives the existing bean instance reference or create one. First it searches for an bean instance, but if it does not find one then it creates a new bean.
Consider this,
<jsp:useBean id=”person” class=”foo.person” scope=”request”/>
This above live is converted into servlet like this,
Public void _jspService(){
foo.person=null;
synchronized(request){
Person=(foo.person)_jspx_page_context.getAttribute(“person”, pageContext.REQUEST_SCOPE);
If(person==null)
Person=new foo.person();
_jspx_page_context.setAttribute(“person”,person, pageContext.REQUEST_SCOPE);
}}
You can understand how useBean creates new bean class.

Now how to set the values if new instance is created
Now see the SetProperty inside the body of useBean.
<jsp:useBean id=”refname” class=”package.classname” scope=”scopename”>
<jsp:setProperty name=”refname” property=”name” value=”bhabani”/>
</jsp-useBean>
Here see the difference there is no end tag in the useBean(same line).
When the useBean did not find the bean class and creates a new bean, at that time the value is set. otherwise not. Any code inside the body of useBean is conditional. It runs only if a new bean instance is created.

No comments:

Post a Comment