This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:setProperty name="" property="*" equiv for servlet

 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm quite sure there's no equivalent code that I can use for servlets.
Are there any easy alternatives? Did anyone manage to decompile the tag code?
Chu
[ June 19, 2002: Message edited by: Chu Tan ]
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written this method to put in the "bean" class. (not tested yet) Just to see if anyone has any comments.

[ June 19, 2002: Message edited by: Chu Tan ]
 
Bhushan Jawle
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having inspired by you idea I have a working(tested) code for servlet equivalent of jsp:setProperty name="" property="*"
/**
* This method populates bean object properties from the request
* parameters having same name. Hence it is equivalent of
* <jsp:setProperty name="beanId" property="*"/>
* @param HttpRequest The servlet request containing parameters
* @param Bean object implementing Serializable
*/

public void populateBean(HttpServletRequest request, Serializable beanToBePopulated)
{
Enumeration enum = request.getParameterNames();
String name;
String[] values;
int i = 0;
while (enum.hasMoreElements())
{
name = (String)enum.nextElement();
values = request.getParameterValues(name);
for (i = 0; i < values.length; i++)
{
try
{
Class beanClass = beanToBePopulated.getClass();
Class fieldClass = beanClass.getDeclaredField(name).getType();
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = beanClass.getMethod(methodName, new Class[]{fieldClass});
method.invoke(beanToBePopulated, new Object[]{values[i]});
}
catch (NoSuchMethodException e){}
catch (java.lang.reflect.InvocationTargetException e){}
catch (IllegalAccessException e){}
catch (NoSuchFieldException e){}
}
}
}
Any comments are welcome.
Regards,
Bhushan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic