我在UPDATE servlet中遇到了一些麻煩。Servlet沒有使用新數據更新數據庫
我試圖更新我的分貝,但它只是沒有發生。我不熟悉Java EE的這一章。
**注意:我只是遇到了UpdateServlet問題,因爲我不知道如何從JSP獲取修改的數據,以便將它發送到DAO,然後更新數據庫。剩下的就是確定
目的:當用戶點擊「更新」按鈕(如下截圖)...
...的JSP將請求轉發到「更新用戶」頁面(如下圖),在那裏他將能夠修改附加到電子郵件(這是primaary鍵)的名字和姓氏(下圖)......
我的問題是:我如何實現從會話獲取User對象的UpdateUserServlet(請參閱下面的代碼)代碼,並使用新的姓氏和名字更新數據庫。
的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>
擊中「更新按鈕」這個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> ....
更新servlet。需要這個幫助。
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);
}
}
的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);
}
}
}
如果用戶點擊提交按鈕,會發生什麼?是servlet命中?單擊提交後,servlet中每個參數的值是什麼? – zerocool
當用戶單擊提交時,會調用UpdateUserServlet,並且DB中的表格行獲取用戶輸入的新名稱和名字。之後,UpdateUserServlet將請求轉發回主Servlet(DisplayUserServlet),以便在更新用戶後顯示usersList –
現在,當我在鍵入新名稱和名字後單擊提交時,轉發到UserList的工作完美,除了名稱和名字沒有在數據庫中更新。 –