Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way of populating Javabeans based on request parameters

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");
    }
like image 828
Simon Avatar asked Feb 23 '11 20:02

Simon


People also ask

How do you populate a bean?

In order to populate the bean, first create the bean, then call the populate method. Request is the HttpServletRequest object that the servlet is passed in doGet and doPost . If you are using BeanUtilites from coreservlets, call the populate method with the bean and the request. BeanExample myBean = new BeanExample();

What are the methods correct for JavaBean?

To access the JavaBean class, we should use getter and setter methods.

What do you mean by JavaBeans explain in detail with example?

JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a java class that should follow following conventions: Must implement Serializable. It should have a public no-arg constructor. All properties in java bean must be private with public getters and setter methods.


1 Answers

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"));
like image 167
BalusC Avatar answered Oct 26 '22 15:10

BalusC