- 制作一个图书模糊查询界面,输入图书名称的模糊信息,能够显示系统中所有图书的名称和价格,但是页面不刷新,结果在页面下方显示。
代码
Book.java
package chapter12.beans;
public class Book {
private String id;
private String name;
private double price;
public Book(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
BookDao.java
package chapter12.dao;
import chapter12.beans.Book;
import java.sql.*;
import java.util.ArrayList;
public class BookDao {
public ArrayList<Book> findAllBooks() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("你的数据库", "用户名", "密码");
String sql = "select * from book";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<Book> books = new ArrayList<>();
while (rs.next()) {
books.add(new Book(rs.getString(1), rs.getString(2), rs.getDouble(3)));
}
ps.close();
conn.close();
return books;
}
public ArrayList<Book> fuzzyLookupBook(String name) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("你的数据库", "用户名", "密码");
String sql = "select * from book where name like '%" + name + "%'";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<Book> books = new ArrayList<>();
while (rs.next()) {
books.add(new Book(rs.getString(1), rs.getString(2), rs.getDouble(3)));
}
ps.close();
conn.close();
return books;
}
}
fuzzyLookupBookServlet.java
package chapter12.servlet;
import chapter12.beans.Book;
import chapter12.dao.BookDao;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
@WebServlet(name = "fuzzyLookupBookServlet", urlPatterns = {"/fuzzyLookupBookServlet"})
public class fuzzyLookupBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
BookDao bookDao = new BookDao();
try {
ArrayList<Book> books = bookDao.fuzzyLookupBook(name);
HttpSession session = request.getSession();
session.setAttribute("books", books);
ServletContext application = this.getServletContext();
application.getRequestDispatcher("/chapter12/12_2/list.jsp").forward(request, response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
search.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>插入数据</title>
<script src="ajax.js" lang="javascript"></script>
</head>
<body>
<form name="searchForm">
请输入书名:<input name="name" type="text">
<input type="button" value="查找" onclick="search()">
</form>
<div id="resultDiv"></div>
</body>
</html>
ajax.js
function search() {
let name = document.searchForm.name.value
let xmlHttp = new XMLHttpRequest()
let url = `/JavaWebCDUT/fuzzyLookupBookServlet?name=${name}`
xmlHttp.open("POST", url, true)
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
resultDiv.innerHTML = xmlHttp.responseText
} else {
resultDiv.innerHTML += "正在查找,请稍候……"
}
}
xmlHttp.send()
}
list.jsp
<%@ page import="chapter12.dao.BookDao" %>
<%@ page import="chapter12.beans.Book" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
ArrayList<Book> books = (ArrayList<Book>) session.getAttribute("books");
%>
<table>
<tr>
<th>图书ID</th>
<th>图书名称</th>
<th>图书价格</th>
</tr>
<%
if (books != null) {
for (Book book : books) {
out.println(String.format("<tr><td>%s</td><td>%s</td><td>%.2f</td></tr>", book.getId(), book.getName(), book.getPrice()));
}
}
%>
</table>
</body>
</html>
|