jsp实现1、新书入库,完成图书的添加操作。添加成功返回图书列表页面。 2、图书信息修改。 3、删除图书(单个删除与批量删除功能)。 4、图书列表显示,完成图书查询功能,可以实现按照作者、书名查询图书
时间: 2023-06-16 19:07:53 浏览: 89
好的,这些功能都可以通过 JSP 和 Servlet 实现。下面是一个简单的示例代码,供参考。
1. 新书入库
添加图书的页面(add.jsp):
```html
<form action="addBook" method="post">
<label>书名:</label>
<input type="text" name="title"><br>
<label>作者:</label>
<input type="text" name="author"><br>
<label>ISBN:</label>
<input type="text" name="isbn"><br>
<button type="submit">添加</button>
</form>
```
处理添加图书请求的 Servlet(AddBookServlet.java):
```java
@WebServlet("/addBook")
public class AddBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String title = request.getParameter("title");
String author = request.getParameter("author");
String isbn = request.getParameter("isbn");
// TODO: 将图书信息保存到数据库中
response.sendRedirect("bookList.jsp");
}
}
```
2. 图书信息修改
修改图书的页面(edit.jsp):
```html
<form action="editBook" method="post">
<input type="hidden" name="id" value="<%= book.getId() %>">
<label>书名:</label>
<input type="text" name="title" value="<%= book.getTitle() %>"><br>
<label>作者:</label>
<input type="text" name="author" value="<%= book.getAuthor() %>"><br>
<label>ISBN:</label>
<input type="text" name="isbn" value="<%= book.getIsbn() %>"><br>
<button type="submit">保存</button>
</form>
```
处理修改图书请求的 Servlet(EditBookServlet.java):
```java
@WebServlet("/editBook")
public class EditBookServlet extends HttpServlet {
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 isbn = request.getParameter("isbn");
// TODO: 更新图书信息到数据库中
response.sendRedirect("bookList.jsp");
}
}
```
3. 删除图书
处理删除图书请求的 Servlet(DeleteBookServlet.java):
```java
@WebServlet("/deleteBook")
public class DeleteBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] ids = request.getParameterValues("id");
// TODO: 根据 id 删除图书信息
response.sendRedirect("bookList.jsp");
}
}
```
4. 图书列表显示与查询
图书列表页面(bookList.jsp):
```html
<form action="searchBook" method="get">
<label>按作者查询:</label>
<input type="text" name="author"><br>
<label>按书名查询:</label>
<input type="text" name="title"><br>
<button type="submit">查询</button>
</form>
<table>
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>ISBN</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<% for (Book book : bookList) { %>
<tr>
<td><%= book.getId() %></td>
<td><%= book.getTitle() %></td>
<td><%= book.getAuthor() %></td>
<td><%= book.getIsbn() %></td>
<td>
<a href="edit.jsp?id=<%= book.getId() %>">编辑</a>
<a href="deleteBook?id=<%= book.getId() %>">删除</a>
</td>
</tr>
<% } %>
</tbody>
</table>
```
处理图书查询请求的 Servlet(SearchBookServlet.java):
```java
@WebServlet("/searchBook")
public class SearchBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String author = request.getParameter("author");
String title = request.getParameter("title");
// TODO: 根据作者和书名查询图书信息
request.setAttribute("bookList", bookList);
request.getRequestDispatcher("bookList.jsp").forward(request, response);
}
}
```
阅读全文