• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

jsp:useBean type is not working?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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}"/>
 
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you checked your log file?
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    MInu
    Ranch Hand
    Posts: 517
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 284
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 517
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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...
     
    Ranch Hand
    Posts: 951
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Frederic Esnault
    Ranch Hand
    Posts: 284
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 517
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    now it is clear... Thanks for your help..
     
    Frederic Esnault
    Ranch Hand
    Posts: 284
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome, happy it helped
     
    The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic