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");
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…