eclipse基于jsp的图书查询实现
时间: 2023-12-11 07:02:23 浏览: 93
实现这个功能,你需要先创建一个基于 JSP 的 Web 应用程序项目。然后,你需要创建一个包含图书信息的数据库,并将其连接到你的项目中。
接下来,你可以创建一个 JSP 页面,这个页面将会包含一个表单,用户可以在其中输入图书名称或者作者名字进行查询。使用 JSP 和 JSTL 标签库,你可以通过 JDBC 连接到数据库,并从中获取数据。查询结果将会显示在 JSP 页面上。
以下是一个示例代码:
index.jsp:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书查询</title>
</head>
<body>
<h1>图书查询</h1>
<form method="GET" action="search.jsp">
<label for="bookName">书名:</label>
<input type="text" name="bookName" id="bookName" />
<br />
<label for="author">作者:</label>
<input type="text" name="author" id="author" />
<br />
<input type="submit" value="查询" />
</form>
</body>
</html>
```
search.jsp:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书查询结果</title>
</head>
<body>
<h1>图书查询结果</h1>
<table>
<tr>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>出版社</th>
</tr>
<c:forEach var="book" items="${books}">
<tr>
<td>${book.bookName}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>${book.publisher}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
Book.java:
```java
public class Book {
private String bookName;
private String author;
private double price;
private String publisher;
// 省略 getter 和 setter 方法
}
```
SearchServlet.java:
```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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/search")
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=UTF-8";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bookName = request.getParameter("bookName");
String author = request.getParameter("author");
List<Book> books = new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement ps = conn.prepareStatement("SELECT * FROM books WHERE bookName LIKE ? AND author LIKE ?");
ps.setString(1, "%" + bookName + "%");
ps.setString(2, "%" + author + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setBookName(rs.getString("bookName"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
book.setPublisher(rs.getString("publisher"));
books.add(book);
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
request.setAttribute("books", books);
request.getRequestDispatcher("/search.jsp").forward(request, response);
}
}
```
在这个示例代码中,我们创建了一个包含两个 JSP 页面和一个 Servlet 的 Web 应用程序。当用户在 index.jsp 页面中输入图书名称或者作者名字并提交表单后,SearchServlet 将会从数据库中获取匹配的图书信息,并将之显示在 search.jsp 页面上。
阅读全文