Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

about getting an object in a jsp  RSS feed

 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

we write the following script to get a primitive or a string member from a bean object

<jsp:getProperty name="objectname" property="membername" />

what if the member itself is an object of another class? how to you get it? after making changes, how do you set it?

jose.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37249
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,
Take a look at the jsp:usebean tag. It lets you declare which bean class you want and access all the properties (via getters and setters.)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get property will always give you a string.
If the bean's propery type is a primitive, getProperty will coerce it to a string value. If it's an object it will call the object's toString method.

If you need to work with object types in your JSPs, you will need to resort to scriptlets. A better approach is to deal with these objects from a servlet and then forward to a JSP that does nothing but handle 'look and feel' issues.

Look up 'Model, View, Controller' or MVC for more infomation.
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use EL expressions for accessing the properties of an object property of an Object....
Suppose you have an Object Person with a property Company which is also an object and you want to display name of the company, then

<jsp:useBean id="person" class="xyz.Person" />
${person.company.name}

this will display person's company's name...
The only requirement for this to work is you have to have jsp2.0... because EL(Expression Language) was introduced in JSP2.0
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only requirement for this to work is you have to have jsp2.0... because EL(Expression Language) was introduced in JSP2.0


The EL can also be used in a limited fashion under JSP 1.x via JSTL 1.0.

But it sounds like the OP wanted to obtain the property not to emit it to the output stream, which either of <jsp:getProperty/> or the EL reference will do, but to perform some processing upon it.

And in that case, Ben's point should be well taken. Stop thinking of JSP pages as somewhere that you perform data processing and start thinking of them as a means to show data that has already been processed somewhere (usually a controller servlet) more suited to data processing.
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the concern to answer the question.

yea, when we think about MVC, we cant speak about manipulating an object from jsp. i just wanted to know if there's a way.

Thanks anyways !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!