aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean type is not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean type is not working?" Watch "jsp:useBean type is not working?" New topic
Author

jsp:useBean type is not working?

Selvaraj Subramanian
Ranch Hand

Joined: Jul 15, 2005
Posts: 37
Hi,
I am using the following code in the JSP file (useBeanTest.jsp)

<jsp:useBean id="emp" type="sample.Employee" scope="request">
<jsp:getProperty name="emp" property="empName"/>
<jsp:getProperty name="emp" property="empDept"/>
</jsp:useBean>

and in the Action class (TestAction.java)

Employee employee = new Raj();
employee.setEmpName("Selvaraj");
employee.setEmpId("112");
employee.setEmpDept("r&d");

request.setAttribute("emp", employee);

Here Employee is an abstract class which is being inherited by Raj...

I am not getting any output or error, what is the reason?

But the same code is working through EL like
<c ut value="${emp.empName}"/>


Selvaraj.S<br />SCJP 1.4 -83%<br />SCWCD 1.4 - 85%<br />SCEA 4 Part 1(310-051) - 89%
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Have you checked your log file?


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
Hi !

You make one of the "watch out" mistakes of the SCWCD exam.
And you make one mistake which is quite bad.

1. Watch out
The jsp:useBean element body is evaluated IF AND ONLY IF the following two conditions are verified:
  • The attribute useBean looks for does not exist;
  • An attribute object is created.


  • In your case, assuming you have no error, the attribute DOES exist (see 2.). So the body of your jsp:useBean element is not evaluated. Hence, you have no output.

    2. type attribute rule
    The type attribute of the jsp:useBean element should ONLY be used alone if the attribute you want to retrieve already exists. If it's not found, one will not be created, you'll get an exception.

    From the spec :
    If the object is not found in the specified scope and neither class nor beanName
    are given, a java.lang.InstantiationException shall occur. This completes the
    processing of this jsp:useBean action.

    In your statement, there is a problem, because you use the type attribute alone (which is legal if the attribute exists) but you put a body in the useBean element, which means you expect the attribute NOT to exist.

    Hope it's clear.
    [ March 23, 2006: Message edited by: Frederic Esnault ]

    SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
    MInu
    Ranch Hand

    Joined: Oct 09, 2003
    Posts: 517
    Frederic ,
    so if we use type without a class the bean name must exist and the type must not be abstract and have a public no arg constructor...plz clarify

    Thanks
    Frederic Esnault
    Ranch Hand

    Joined: Feb 13, 2006
    Posts: 284
    No, it's not the bean name, the actual attribute (object) must already exist in one of the four scopes.

    Let's see use cases :

    class attribute : defines the type of the actual object attribute. It must be a concrete class (fully qualified), and this class MUST have a no-arg constructor, because if the attribute is not found and class is used, an attribute will be created, using this no-arg cstr.

    type attribute : This attribute is optional and is used to define the type of the reference to the actual attribute object. It must be a concrete or abstract class, or an interface.

    Usage : Possible combinations are :
  • class alone;
  • class with type;
  • type alone;


  • If class is used alone, must be a concrete class with a no-arg cstr.

    If class is used with type, type must be :
  • the class itself;
  • a superclass of the class;
  • an interface implemented by the class;

  • In this case, the attribute may or may not exist, as if it doesn't exist, one will be created using the class no-arg cstr.

    If type is used alone, the same rule applies to its value : it must be the same class as, a superclass of, or an interface implemented by the actual attribute object class. Used alone, the attribute MUST exist, otherwise you'll get an exception (one will NOT be created).


    JSP translation and class/type meaning :One last point to explain the class/type role.
    You know your JSP is translated to a servlet. The jsp:useBean element is translated to a bean creation (along with a search in scope I don't cover).

    If you use class alone and give it (for example) the value MyBean, then the translated code will look like this :



    If you use type (set to MyBeanSuperClass) along with the class attribute set before, you get :



    As you can see :
  • type becomes the type of the reference to the actual object;
  • class becomes the type of the actual object.


  • Hope this helps.
    [ March 23, 2006: Message edited by: Frederic Esnault ]
    MInu
    Ranch Hand

    Joined: Oct 09, 2003
    Posts: 517
    Sorry i did'nt get the concept clear.

    Please have a look at this code

    <jsp:useBean id="person" type="servlettest.Person" scope="request"/>
    <jsp:getProperty name="person" property="name"/>

    This method displays the name property of the bean object 'person'.
    Here i have used 'type' without a class but the type is not an abstract class and the bean object (bean name) exists in the request scope.So my conclusion is, if type is used alone...the bean object must exist and the type must not be abstract and have a no arg constructor.

    please correct me it is wrong.
    Thanks.
    Selvaraj Subramanian
    Ranch Hand

    Joined: Jul 15, 2005
    Posts: 37
    Thanks Frederic! Yes, I remember... the body part will not be evaluated if the attribute already exists. I forgot it because I was keep on changing useBean tag & testing it for different results...
    Narendra Dhande
    Ranch Hand

    Joined: Dec 04, 2004
    Posts: 950
    Originally posted by Vinod NS:
    Sorry i did'nt get the concept clear.

    Please have a look at this code

    <jsp:useBean id="person" type="servlettest.Person" scope="request"/>
    <jsp:getProperty name="person" property="name"/>

    This method displays the name property of the bean object 'person'.
    Here i have used 'type' without a class but the type is not an abstract class and the bean object (bean name) exists in the request scope.So my conclusion is, if type is used alone...the bean object must exist and the type must not be abstract and have a no arg constructor.



    Here the type attribute can be abstract. But the class(bean) identified by the id person in the request scope must be the instance of the subclass of abstract class Person and follow the JavaBean rules. Another thing is that as you have not specified the class attribute you can only access the properties which are definded in the abstarct superclass defination. New properties defined in the subclass can not be displayed using getProperty. (But tomact allow this)


    Hope it help

    Thanks


    Narendra Dhande
    SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
    Frederic Esnault
    Ranch Hand

    Joined: Feb 13, 2006
    Posts: 284
    Originally posted by Vinod NS:
    Sorry i did'nt get the concept clear.

    Please have a look at this code

    <jsp:useBean id="person" type="servlettest.Person" scope="request"/>
    <jsp:getProperty name="person" property="name"/>

    This method displays the name property of the bean object 'person'.
    Here i have used 'type' without a class but the type is not an abstract class and the bean object (bean name) exists in the request scope.So my conclusion is, if type is used alone...the bean object must exist and the type must not be abstract and have a no arg constructor.

    please correct me it is wrong.


    First of all, I discussed about the code as seen in the first post, where the jsp:getProperty is IN THE BODY of the jsp:useBean action. Your question is not the same.

    Secondly, your conclusion is half incorrect. If type is used alone, the bean object must exist; this is correct.

    But, it may be abstract and doesn't need a no-arg cstr.
    Remember, type defines the reference type :
    ReferenceType ref = new ActualObjectType();

    The class attributes defines the actual object type :
    ReferenceType ref = new ActualObjectType();

    If type is used alone, you assume the object exists, so you don't need to use a constructor. You just take the actual object and reference it with a reference of the type you specify. As you learned from J2SE, the reference may be of the same class, a superclass, or an implemented interface.

    Is it clearer?
    [ March 23, 2006: Message edited by: Frederic Esnault ]
    MInu
    Ranch Hand

    Joined: Oct 09, 2003
    Posts: 517
    now it is clear... Thanks for your help..
    Frederic Esnault
    Ranch Hand

    Joined: Feb 13, 2006
    Posts: 284
    You're welcome, happy it helped
     
    jQuery in Action, 2nd edition
     
    subject: jsp:useBean type is not working?