The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP.
The syntax for this is as follows-
Declare and initialize a bean attribute with <jsp:useBean>
Explanation of this-
jsp:useBean -> Identifies the standard action.
id="person" -> It declares the identifier for the bean object created.This is equivalent to the name used in servlet like request.setAttribute("person",p);
class -> Declares the class type(We need fully classified name of class.)
scope -> Identifies the attribute scope for the bean object.
Get a bean attribute's property value using <jsp:getProperty>
jsp:getProperty -> Identifies the standard action.
name="person" -> Identifies the bean object.This will match the value of id of useBean tag.
property -> Identifies the property name.
Can <jsp:useBean> create a bean ??
Yes,it can.It first searches for an existing thing.But if it does not find one,it creates one.
If the container creates a bean,it wont have its properties set. This can be bad in some cases.
So there shud be something to set the properties.yes you are right,it is <jsp:setProperty>.
<jsp:setProperty>
When there is a get,there should be a set.
The syntax for this is-
Now the problem is if the bean already existed,then it can reset the value of property of the bean.
So there shud be some way to skip this if the bean has already some value set.
Yeah sure there is.
If you put your <jsp:setProperty> inside the body of <jsp:useBean>,the property setting is conditional.
Or in other words,we can say the property values will set only if the new bean is created.
Now after doing all this,one thing comes to mind.What if we can directly pass the parameters in the constructor
of bean instead of setting things.So The answer to this is -Beans cant have constructors with arguments.Simply enough.:)
Can we make polymorphic bean references????
When we write <jsp:useBean>,its class attribute decides the class of the new object.
It also determines the type of the reference variable used in the generated servlet.
For example.
for this,the generated servlet code will be-
But what if we want the reference to be different from the actual object type???
We will change the Person class to be abstract and we will make a concrete subclass Employee.
Now we want the reference of Person and object of Employee.
Now Comes the TYPE attribute in picture.
now if you do the same thing as before like
Our new jsp with type
Generated Servlet
Now the question arises can we use type without class???
it works perfectly.
CASE 2- if person attribute is not existing in page scope
java.lang.InstantiationException.
Dont confuse type with class.
type=reference type
class=object type.
The syntax for this is as follows-
Declare and initialize a bean attribute with <jsp:useBean>
<jsp:useBean id="person" class="prince.Person" scope="request" />
Explanation of this-
jsp:useBean -> Identifies the standard action.
id="person" -> It declares the identifier for the bean object created.This is equivalent to the name used in servlet like request.setAttribute("person",p);
class -> Declares the class type(We need fully classified name of class.)
scope -> Identifies the attribute scope for the bean object.
Get a bean attribute's property value using <jsp:getProperty>
<jsp:getProperty name="person" property="name" />Explanation of this-
jsp:getProperty -> Identifies the standard action.
name="person" -> Identifies the bean object.This will match the value of id of useBean tag.
property -> Identifies the property name.
Can <jsp:useBean> create a bean ??
Yes,it can.It first searches for an existing thing.But if it does not find one,it creates one.
If the container creates a bean,it wont have its properties set. This can be bad in some cases.
So there shud be something to set the properties.yes you are right,it is <jsp:setProperty>.
<jsp:setProperty>
When there is a get,there should be a set.
The syntax for this is-
<jsp:setProperty name="person" property="name" value="prince" />
Now the problem is if the bean already existed,then it can reset the value of property of the bean.
So there shud be some way to skip this if the bean has already some value set.
Yeah sure there is.
If you put your <jsp:setProperty> inside the body of <jsp:useBean>,the property setting is conditional.
Or in other words,we can say the property values will set only if the new bean is created.
<jsp:useBean id="person" class="prince.Person" scope="request"> <jsp:setProperty name="person" property="name" value="prince" /> </jsp:useBean>
Now after doing all this,one thing comes to mind.What if we can directly pass the parameters in the constructor
of bean instead of setting things.So The answer to this is -Beans cant have constructors with arguments.Simply enough.:)
Can we make polymorphic bean references????
When we write <jsp:useBean>,its class attribute decides the class of the new object.
It also determines the type of the reference variable used in the generated servlet.
For example.
<jsp:useBean id="person" class="prince.Person" scope="page" />
for this,the generated servlet code will be-
prince.Person person=new prince.Person(); .....
But what if we want the reference to be different from the actual object type???
We will change the Person class to be abstract and we will make a concrete subclass Employee.
Now we want the reference of Person and object of Employee.
package prince; public abstract class Person { private String name; public void setName(String name) { this.name=name; } public String getName() { return name; } }
package prince; public class Employee extends Person { private int empID; public void setEmpID(int empID) { this.empID=empID; } public int getEmpID() { return empID; } }
Now Comes the TYPE attribute in picture.
now if you do the same thing as before like
<jsp:useBean id="person" class="prince.Person" scope="page" />it will throw an exception java.lang.InstantiationException.Since Person is now an abstarct class,we can not create an object of that.
Our new jsp with type
<jsp:useBean id="person" class="prince.Person" type="prince.Employee" scope="page" />
Generated Servlet
prince.Person person=null; person=new prince.Employee();Now here the reference is of abstract type and object is of concrete class type.
Now the question arises can we use type without class???
<jsp:useBean id="person" type="prince.Person" scope="page" />CASE 1- if person attribute already exists in page scope
it works perfectly.
CASE 2- if person attribute is not existing in page scope
java.lang.InstantiationException.
Dont confuse type with class.
type=reference type
class=object type.