Forums Register Login

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

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1406 times.
Similar Threads
Why a JFrame's appearence on Windows is different from its on Linux?
B&S: Passed 373/400
EJB bean implementing local/remote interfaces
How to implement: java code --> exe on Windows?
EAR/WAR/JAR Architecture
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 02:55:41.