• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet - JavaBean properties

 
Jaime Garcia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to use a JavaBean from a Servlet like JSP dose ?
I mean, in a JSP you can use:
<jsp:setProperty name="language" property="*"/>
to set all properties of a form. Is there a way to do this in a Servlet ?
A JSP is really a servlet, (thats what I hear about), the code that tomcat generates (from a JSP) is something like this:
//Just creates the bean, I am not sure wath dose "Beans.instantiate"
//differs from just "new League()" if my bean
//just have setter and getter functions
league = (sports.SportActivities.League) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "sports.SportActivities.League");
pageContext.setAttribute("league", league, PageContext.REQUEST_SCOPE);
//I guess here is where it asign all properties.
//Maybe it use some of java.lang.reflect, maybe not (just an idea).
JspRuntimeLibrary.introspect(pageContext.findAttribute("league"), request);
My question, (again) Is there a way to do this in a Servlet or I have to use the JspRuntimeLibrary ?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to use a JavaBean from a Servlet like JSP dose ?
Yes.

I mean, in a JSP you can use:
<jsp:setProperty name="language" property="*"/>
to set all properties of a form. Is there a way to do this in a Servlet ?

Doing this is like setting all the properties of the bean in a single action. This scriptlet does the same:

and Yes, this is called introspection.

Introspection, the process by which a builder tool analyzes how a Bean works, differentiates Beans from typical Java classes. Because Beans are coded with predefined patterns for their method signatures and class definitions, tools that recognize these patterns can "look inside" a Bean and determine its properties and behavior.
Builder environments, and some runtime situations need to identify the properties, events, and methods that a bean supports. This process is called introspection.

so you're capable of doing this because your form elemnts (for example) match *exactly* with your bean properties.
you also might want to look at this site, it explains how to use it.
hope this helps
[ August 26, 2003: Message edited by: Andres Gonzalez ]
 
Gary Mann
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to check out the Jakarta Commons beanutils package that includes code to populate JavaBeans via reflection.
http://jakarta.apache.org/commons/beanutils/api/index.html
See org.apache.commons.beanutils.BeanUtils.populate()
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gray Man,

Welcome to JavaRanch!! Please change your name to be compliant with JavaRanch's naming policy. It should not be obviously fictitious.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!