• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing JSP bean properties to console

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering how we can print a bean's field/properties to the console. Let's say, I have a Session scoped bean object with name: SessBean1. I can write a scriptlet to loop all of its properties using System.out.println(session.getAttribute(SessBean1).getProperty()) to print any property using getter methods of the bean object to the console. Isn't there an easier way to do this as in using EL.

I can easily access a property name I know using EL or struts-html or struts-bean tags. The problem comes when the property is dynamic and I have to construct a getter method on this object in the scriptlet.

Here's an example:



to access/print any property dynamically of the bean.

But how can I do it in a scriptlet without using getter method of the bean object. Are there any other ways I can print fields/properties of the bean object to my console?

This appears like a question that might have been asked previously, but I always fail searching for my problem.

[ March 13, 2008: Message edited by: Rama Krishna ]

[ March 13, 2008: Message edited by: Rama Krishna ]
[ March 13, 2008: Message edited by: Rama Krishna ]
 
Ranch Hand
Posts: 72
Scala Monad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To print an Array using JSTL & EL,



To print a collection of beans,



You need to have the matching getters for the properties in the bean class.
That is, getProperty1(), getProperty2()

You dont need to mention the scope of the variables. EL will search for the variables in the scopes in order (page, request, session, application). If you want, you can mention the scopes using EL implicit objects for each scope.
[ March 13, 2008: Message edited by: Marimuthu Madasamy ]
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to print to my console.. similar to the System.out.println() kind of equivalent. Let me check if this does this.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The EL is designed to emit to the response. If you want to do something else, perhaps now is a great time to explore custom tags or custom EL functions.
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rama,

-I don't understand why you want to write to the console (why not to proper logfile?), but even in a JSP you can use System.out
It's probably easier/better to put the logging outside of your jsp, in a servlet or ..

-the Jakarta Commons - BeanUtils project has a class BeanUtils, with a describe method that returns all properties of an object in a map.
Use the property name to get it's value.

-if you cannot use this package, make a usefull toString() methode for the objects that you want to print, and call this toString() method.

succes,
Herman
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Thanks for your quick suggestions. How about a quick dirty check on the output. I thought printing to console does the job for me and I began using System.out.println();

and posted to find out if there was a way to print out a particular bean properties or fields just like using EL to display on the jsp page.

-Cheers
Rama
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, I was able to print all the form beans and their properties directly as follows:

<bean:define id="someTmpBean" name="MyActionFormName" property="SomeBeanTypeList" type="java.util.List<database.SomeBeanType>">
</bean:define>

<%
System.out.println("List size: " + someTmpBean.size());

for(database.SomeBeanType someBean: someTmpBean)
{
System.out.println("fieldName: " + someBean.getFieldName() + ", datatype " + someBean.getDataType() + ", formatString" + someBean.getFormatString());
}

%>

As again, this is not suggested at all.

-Cheers
Rama
[ May 03, 2008: Message edited by: Rama Krishna ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic