8

I have a simple person class:

package simpleApp.entities;

public class Person {
    private String name;
    private String secondname;

    public void setSecondname(String cognome) {
        this.secondname = cognome;
    }
    public String getSecondname() {
        return secondname;
    }
    public void setName(String nome) {
        this.name = nome;
    }
    public String getName() {
        return name;
    }

}

and a simple html page:

<html>
<body>

    <form action="/simpleApp/person/" method="POST">
        name: <input type="text" name="name"><br>
        second name: <input type="text" name="secondname"><br>
        <input type="submit">
    </form>

</body>
</html>

and a simple servlet:

public class Person extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Person() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //LOOK HERE:
        simpleApp.entities.Person p = new simpleApp.entities.Person();

        p.setName(request.getParameterValues("name")[0]);
        p.setSecondname(request.getParameterValues("secondname")[0]);

        response.sendRedirect("/simpleApp/index.html");
    }

}

Is there a way to automate the parameter setting?

Something magic like

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        simpleApp.entities.Person p = new simpleApp.entities.Person();

        Magic.justSetEverything(p, request);

//      NOT NEEDED ANYMORE!!! MUAHAHAHA more time for coffee
//      p.setName(request.getParameterValues("name")[0]);
//      p.setSecondname(request.getParameterValues("secondname")[0]);

        response.sendRedirect("/simpleApp/index.html");
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Simon
  • 422
  • 5
  • 19
  • This could certainly be done through the reflection API, but the question is if it's not more trouble than it's worth, unless you encounter this situation very often or have an enormous number of attributes to set. Also keep in mind that you probably want to do some validation ("was the parameter given?" "is the value valid for this field?") – johusman Feb 23 '11 at 20:17
  • 1
    For your sanity, rename your servlet to `PersonServlet` or something. – Jeremy Feb 23 '11 at 20:17
  • Thanks johusman, I was hoping there is already something. The validation and other logic is done later on on the instance of the class. – Simon Feb 23 '11 at 20:18
  • Jeremy, it's only an example I wrote specifically for this SO post :) – Simon Feb 23 '11 at 20:20

2 Answers2

20

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"));
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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);
aristotll
  • 8,694
  • 6
  • 33
  • 53