Note that there are some explanatory texts on larger screens.

plurals
  1. POEasy way of populating Javabeans based on request parameters
    primarykey
    data
    text
    <p>I have a simple person class:</p> <pre><code>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; } } </code></pre> <p>and a simple html page:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form action="/simpleApp/person/" method="POST"&gt; name: &lt;input type="text" name="name"&gt;&lt;br&gt; second name: &lt;input type="text" name="secondname"&gt;&lt;br&gt; &lt;input type="submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and a simple servlet:</p> <pre><code>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"); } } </code></pre> <p>Is there a way to automate the parameter setting?</p> <p>Something magic like</p> <pre><code> 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"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload