title: Servlet is not Updating DB with new data
body: <p>Im having some little troubles with the UPDATE servlet.</p>
  
  <p>Im trying to update my db but its just not happening. I'm new to this chapter of Java EE.</p>
  
  <p>**NB: I'm just having trouble with the UpdateServlet because i dont know how to get the modified datas from the JSP in order to send it to the DAO and then to update the DB. The rest is OK</p>
  
  <p><strong>The purpose :</strong> When the user hits the "Update" button (screenshot below)...</p>
  
  <p><img src="https://i.stack.imgur.com/P1MFz.png" alt="enter image description here"> </p>
  
  <p>... the JSP forwards the request to the "update user" page (below) where he'll be able to modify the first and last name attached to the email (which is the primaary key)(screenshot below)...</p>
  
  <p><img src="https://i.stack.imgur.com/kAlVN.png" alt="enter image description here"></p>
  
  <p>My question is : how do i implement the <strong>UpdateUserServlet</strong> (see code below) code that gets the User object from the session and updates the database with the new first and last name.</p>
  
  <p>The JSP that displays the User List </p>
  
  <pre><code>&lt;body&gt;
  
  &lt;h1&gt;Users List&lt;/h1&gt;
  
  &lt;table cellpadding="5" border=1&gt;
  
    &lt;tr valign="bottom"&gt;
      &lt;th&gt;First Name&lt;/th&gt;
      &lt;th&gt;Last Name&lt;/th&gt;
      &lt;th&gt;Email Address&lt;/th&gt;
    &lt;/tr&gt;
  
    &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
    &lt;c:forEach var="user" items="${users}"&gt;
    &lt;tr valign="top"&gt;
      &lt;td&gt;&lt;p&gt;${user.firstName}&lt;/td&gt;
      &lt;td&gt;&lt;p&gt;${user.lastName}&lt;/td&gt;
      &lt;td&gt;&lt;p&gt;${user.emailAddress}&lt;/td&gt;
      &lt;td&gt;&lt;a href="displayUser?emailAddress=${user.emailAddress}"&gt;Update&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;a href="deleteUser?emailAddress=${user.emailAddress}"&gt;Delete&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/c:forEach&gt;
  
  &lt;/table&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>After hitting the "Update button" this JSP below takes over.</p>
  
  <pre><code>....
  
  &lt;body&gt;
  
  &lt;h1&gt;Update User&lt;/h1&gt;
  
  &lt;form action="updateUser" method="post"&gt;
  &lt;table cellspacing="5" border="0"&gt;
      &lt;tr&gt;
          &lt;td align="right"&gt;First name:&lt;/td&gt;
          &lt;td&gt;&lt;input type="text" name="firstName" 
                  value="${user.firstName}"&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td align="right"&gt;Last name:&lt;/td&gt;
          &lt;td&gt;&lt;input type="text" name="lastName" 
                  value="${user.lastName}"&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td align="right"&gt;Email address:&lt;/td&gt;
  
          &lt;td&gt;${user.emailAddress}&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="button" value="Submit"&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  
  &lt;/body&gt; ....
  </code></pre>
  
  <p><strong>The Update servlet. Ineed help with this one.</strong></p>
  
  <pre><code>package user;
  
  import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  import business.User;
  import data.UserDB;
  
  public class UpdateUserServlet extends HttpServlet
  {
  
      protected void doPost(HttpServletRequest request, 
              HttpServletResponse response) 
              throws ServletException, IOException
      {
          String firstName = request.getParameter("firstName");
          String lastName = request.getParameter("lastName");
          String emailAddress = request.getParameter("emailAddress");
  
          User user = new User();
  
  
  
          HttpSession session = request.getSession();
          session.setAttribute("user", user);
  
          user.setFirstName(firstName);
          user.setLastName(lastName);
          user.setEmailAddress(emailAddress);
  
          UserDB.update(user);
  
  
          // TODO: add code that gets the User object from the session and updates the database
  
          String url = "/displayUsers";
          RequestDispatcher dispatcher =
                getServletContext().getRequestDispatcher(url);
          dispatcher.forward(request, response);
      }
  }
  </code></pre>
  
  <p>The DAO </p>
  
  <pre><code>package data;
  
  import java.sql.*;
  import java.util.ArrayList;
  
  import business.User;
  
  public class UserDB
  
  {
  
      public static int update(User user) {
          ConnectionPool pool = ConnectionPool.getInstance();
          Connection connection = pool.getConnection();
          PreparedStatement ps = null;
  
          String query = "UPDATE User SET " + "FirstName = ?, " + "LastName = ? "
                  + "WHERE EmailAddress = ?";
          try {
              ps = connection.prepareStatement(query);
              ps.setString(1, user.getFirstName());
              ps.setString(2, user.getLastName());
              ps.setString(3, user.getEmailAddress());
  
              return ps.executeUpdate();
          } catch (SQLException e) {
              e.printStackTrace();
              return 0;
          } finally {
              DBUtil.closePreparedStatement(ps);
              pool.freeConnection(connection);
          }
      }
  
  }
  </code></pre>