简体   繁体   English

Servlet不使用新数据更新数据库

[英]Servlet is not Updating DB with new data

Im having some little troubles with the UPDATE servlet. 我在使用UPDATE Servlet时遇到了一些小麻烦。

Im trying to update my db but its just not happening. 我试图更新我的数据库,但它没有发生。 I'm new to this chapter of Java EE. 我是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. ** NB:我在使用UpdateServlet时遇到了麻烦,因为我不知道如何从JSP获取修改后的数据,以便将其发送到DAO,然后更新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)... ... JSP将请求转发到“更新用户”页面(如下),在此页面中,他将能够修改附加到电子邮件的名字和姓氏(这是主键)(下面的屏幕截图)...

在此处输入图片说明

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. 我的问题是:我如何实现UpdateUserServlet (请参见下面的代码)代码,该代码从会话中获取User对象,并使用新的名字和姓氏更新数据库。

The JSP that displays the User List 显示用户列表的JSP

<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. 点击“更新按钮”后,下面的这个JSP将接管。

....

<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. 更新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 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. 尝试将日志添加到update(User user)方法。 See whether control is coming to this place if atall. 看看是否有控制权来到这个地方。

I found the root cause in the second JSP see the code below. 我在第二个JSP中找到了根本原因,请参见下面的代码。

<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.. Servlet的getParameter(“ emailAddress”)方法实际上获得了一个空值,因为上面的代码中没有参数名称。

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. 请注意,- 输入type =“ text” -不是必需的,因为不必像名称和名字一样修改电子邮件地址。 So i should find a way to show the email address in something else than a input text box. 因此,我应该找到一种在输入文本框之外的其他位置显示电子邮件地址的方法。 But it works now 但现在有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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