简体   繁体   中英

Servlet is not Updating DB with new data

Im having some little troubles with the UPDATE servlet.

Im trying to update my db but its just not happening. I'm new to this chapter of Java EE.

**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

The purpose : When the user hits the "Update" button (screenshot below)...

在此处输入图片说明

... 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)...

在此处输入图片说明

My question is : how do i implement the UpdateUserServlet (see code below) code that gets the User object from the session and updates the database with the new first and last name.

The JSP that displays the User List

<body>

<h1>Users List</h1>

<table cellpadding="5" border=1>

  <tr valign="bottom">
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email Address</th>
  </tr>

  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <c:forEach var="user" items="${users}">
  <tr valign="top">
    <td><p>${user.firstName}</td>
    <td><p>${user.lastName}</td>
    <td><p>${user.emailAddress}</td>
    <td><a href="displayUser?emailAddress=${user.emailAddress}">Update</a></td>
    <td><a href="deleteUser?emailAddress=${user.emailAddress}">Delete</a></td>
  </tr>
  </c:forEach>

</table>

</body>

After hitting the "Update button" this JSP below takes over.

....

<body>

<h1>Update User</h1>

<form action="updateUser" method="post">
<table cellspacing="5" border="0">
    <tr>
        <td align="right">First name:</td>
        <td><input type="text" name="firstName" 
                value="${user.firstName}">
        </td>
    </tr>
    <tr>
        <td align="right">Last name:</td>
        <td><input type="text" name="lastName" 
                value="${user.lastName}">
        </td>
    </tr>
    <tr>
        <td align="right">Email address:</td>

        <td>${user.emailAddress}</td>
    </tr>
    <tr>
        <td></td>
        <td><input type="button" value="Submit"></td>
    </tr>
</table>
</form>

</body> ....

The Update servlet. Ineed help with this one.

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);
    }
}

The DAO

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);
        }
    }

}

Try adding logs to update(User user) method. See whether control is coming to this place if atall.

I found the root cause in the second JSP see the code below.

<tr>
        <td align="right">Email address:</td>

        <td>${user.emailAddress}</td>
    </tr>    

The servlet's getParameter("emailAddress") method was actually getting a null value since there is no parameter name in the code above..

So, it should have been done like this:

<tr>
        <td align="right">Email address:</td>
        <td><input type="text" name="emailaddress" 
                value="${user.emailaddress}">
        </td>
    </tr>

Note that the - input type="text" - is not necessary since the email address doesnt have to be modified like the name and firsname. So i should find a way to show the email address in something else than a input text box. But it works now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM