I have a simple person class:
package simpleApp.entities;
public class Person {
private String name;
private String secondname;
public void set
Yet another way to do it. SpringMvc can auto bind the request for you, but you can also do it mannually.
final WebRequest servletWebRequest = new ServletWebRequest(request);
final WebRequestDataBinder binder = new WebRequestDataBinder(bean);
binder.bind(servletWebRequest);
For that Apache Commons BeanUtils is often used.
BeanUtils.populate(bean, request.getParameterMap());
That's it.
To get a step further, you can adopt a MVC framework which uses Javabeans as models so that you don't need to worry about them at all, such as JSF or Spring MVC.
Unrelated to the concrete question, using getParameterValues()
is clumsy in this specific example. Just use getParameter()
.
p.setName(request.getParameter("name"));
p.setSecondname(request.getParameter("secondname"));