In JSP, how to identify the type of Object present in a List?

14,726

Solution 1

Using JSTL, you can retrieve everything that uses the JavaBean spec - if you want to use getClass() in java, you would use .class in JSTL:

This would write out your classname:

${myList[0].class}

Solution 2

I realize this question is 6 years old; however if anyone searching for how to get the Java class of an Object in JSP finds this question, note that current versions of JSP actually do not allow this notation. You would have to do

${myList[0]['class']}

instead. If you want the class name as a string, this method works well with the .name method mentioned above. You would do

${myList[0]['class'].name}

You can find out more here: https://bz.apache.org/bugzilla/show_bug.cgi?id=50120

Hope this helps someone!

Share:
14,726
Rakesh Juyal
Author by

Rakesh Juyal

Actively seeking new opportunities.

Updated on July 28, 2022

Comments

  • Rakesh Juyal
    Rakesh Juyal almost 2 years

    Is it possible in JSP to get the type of Object in List, just like we do in Java

    myDataBind.getResultsList().get(0).getClass();
    

    or is it possible to achieve something like this:

    if ( myDataBind.getResultsList().get(0) instanceOf MyClass ) {
      doThis;
    }
    

    i don't prefer scriptlets, but if it is not possible to do without scriptlets then Please let me know even that solution too.

    • assuming all objects in list are of same type.
  • hansvb
    hansvb almost 15 years
    Neat. ${myList[0].class.name} would be the classname though (a simple toString will print "class java.lang.String" or "interface java.util.Map")