jsp实现图书信息修改
时间: 2024-05-04 20:21:06 浏览: 11
要实现图书信息修改,需要以下步骤:
1. 编写一个 JSP 页面,可以显示图书列表,并提供修改按钮。
2. 使用 JSTL 标签库从数据库中获取图书列表,并将其显示在 JSP 页面上。
3. 在 JSP 页面中添加修改按钮,并为每个按钮添加一个唯一的 ID。
4. 编写一个 Servlet,用于处理修改请求。Servlet 应该接收来自 JSP 页面的请求,从数据库中获取要修改的图书信息,并将其显示在一个表单中。
5. 在表单中允许用户修改图书信息,并提交修改请求。
6. Servlet 接收修改请求,并将修改后的信息保存到数据库中。
7. 将用户重定向回图书列表页面,以显示更新后的信息。
下面是一个简单的示例代码,用于演示如何修改图书信息:
bookList.jsp:
```
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>图书列表</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>出版日期</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${bookList}" var="book">
<tr>
<td><c:out value="${book.id}"/></td>
<td><c:out value="${book.title}"/></td>
<td><c:out value="${book.author}"/></td>
<td><c:out value="${book.publishDate}"/></td>
<td><c:out value="${book.price}"/></td>
<td><a href="editBook.jsp?id=${book.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
editBook.jsp:
```
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>修改图书</title>
</head>
<body>
<h1>修改图书</h1>
<form method="post" action="editBookServlet">
<input type="hidden" name="id" value="<c:out value="${id}"/>"/>
<p>
<label>书名:</label>
<input type="text" name="title" value="<c:out value="${title}"/>"/>
</p>
<p>
<label>作者:</label>
<input type="text" name="author" value="<c:out value="${author}"/>"/>
</p>
<p>
<label>出版日期:</label>
<input type="text" name="publishDate" value="<c:out value="${publishDate}"/>"/>
</p>
<p>
<label>价格:</label>
<input type="text" name="price" value="<c:out value="${price}"/>"/>
</p>
<p>
<input type="submit" value="保存"/>
</p>
</form>
</body>
</html>
```
editBookServlet.java:
```
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EditBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
String author = request.getParameter("author");
String publishDate = request.getParameter("publishDate");
double price = Double.parseDouble(request.getParameter("price"));
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement stmt = conn.prepareStatement("UPDATE books SET title=?, author=?, publish_date=?, price=? WHERE id=?");
stmt.setString(1, title);
stmt.setString(2, author);
stmt.setString(3, publishDate);
stmt.setDouble(4, price);
stmt.setInt(5, id);
stmt.executeUpdate();
conn.close();
} catch (SQLException e) {
throw new ServletException(e);
}
response.sendRedirect("bookList.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM books WHERE id=?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
request.setAttribute("id", rs.getInt("id"));
request.setAttribute("title", rs.getString("title"));
request.setAttribute("author", rs.getString("author"));
request.setAttribute("publishDate", rs.getString("publish_date"));
request.setAttribute("price", rs.getDouble("price"));
request.getRequestDispatcher("editBook.jsp").forward(request, response);
} else {
throw new ServletException("无法找到 ID 为 " + id + " 的图书");
}
conn.close();
} catch (SQLException e) {
throw new ServletException(e);
}
}
}
```
注意:这里使用了 JSTL 标签库来显示图书列表,如果您不熟悉 JSTL 的使用方法,可以先学习一下。另外,这里的代码只是一个示例,实际应用中需要根据具体情况进行修改和优化。