- 编写图书模糊查询界面,输入图书名称的模糊资料,在界面下方显示图书信息,要求提交给Servlet完成。
解析
在数据库中建个表
然后简单地弄一个book类
public class book {
public String id;
public String name;
public double price;
public book(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}
在search.jsp
中先搭个框架
<form action="一会要创建的Servlet" method="post">
请输入要查找的书名:<input name="name" type="text">
<input type="submit" value="查询">
</form>
<table>
<tr>
<th>图书ID</th>
<th>图书名称</th>
<th>图书价格</th>
<th>操作</th>
</tr>
<!-- 这里一会要插入JSP代码 -->
</table>
新建一个FuzzyLookupServlet
@WebServlet(name = "FuzzyLookupServlet", urlPatterns = {"/fuzzyLookupServlet"})
public class FuzzyLookupServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理响应乱码
response.setContentType("text/html;charset=utf-8");
// 处理请求乱码
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
if (name != null) {
try {
// 1、导入驱动jar包
// 2、注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 3、获取数据库的连接对象
Connection conn = DriverManager.getConnection("你的数据库", "用户名", "你的密码");
// 4、定义sql语句
String sql = "select * from book where name like '%" + name + "%'";
// 5、获取执行sql语句的对象
PreparedStatement ps = conn.prepareStatement(sql);
// 6、执行sql并接收返回结果
ResultSet rs = ps.executeQuery(sql);
// 7、处理结果
ArrayList<book> res = new ArrayList<book>();
while (rs.next()) {
res.add(new book(rs.getString(1), rs.getString(2), rs.getDouble(3)));
}
HttpSession session = request.getSession();
session.setAttribute("result", res); // 把查询到的结果存放在session中
// 8、释放资源
ps.close();
conn.close();
// 9、跳转
request.getRequestDispatcher("/chapter9/9_1/search.jsp").forward(request, response); // 查询完毕后要返回到search.jsp
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
回到search.jsp
,先把form action
补齐,如
<form action="/JavaWebCDUT/fuzzyLookupServlet" method="post">
然后补全JSP代码
<%
ArrayList<book> res = (ArrayList<book>) session.getAttribute("result");
if (res != null) {
%>
<table>
<tr>
<th>图书ID</th>
<th>图书名称</th>
<th>图书价格</th>
<th>操作</th>
</tr>
<%
for (book b : res) {
out.print("<tr>");
out.print("<td>" + b.id + "</td>");
out.print("<td>" + b.name + "</td>");
out.print("<td>" + b.price + "</td>");
out.print(String.format("<td><a href=\"/JavaWebCDUT/addToCartServlet?id=%s\">添加到购物车</a></td>", b.id));
out.print("</tr>");
}
}
%>
完整代码
search.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="chapter9.book" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
<title>模糊查询</title>
</head>
<body>
<form action="/JavaWebCDUT/fuzzyLookupServlet" method="post">
请输入要查找的书名:<input name="name" type="text">
<input type="submit" value="查询">
</form>
<%
ArrayList<book> res = (ArrayList<book>) session.getAttribute("result");
if (res != null) {
%>
<table>
<tr>
<th>图书ID</th>
<th>图书名称</th>
<th>图书价格</th>
<th>操作</th>
</tr>
<%
for (book b : res) {
out.print("<tr>");
out.print("<td>" + b.id + "</td>");
out.print("<td>" + b.name + "</td>");
out.print("<td>" + b.price + "</td>");
out.print(String.format("<td><a href=\"/JavaWebCDUT/addToCartServlet?id=%s\">添加到购物车</a></td>", b.id));
out.print("</tr>");
}
}
%>
</table>
</body>
</html>
FuzzyLookupServlet.java
package chapter9.servlet;
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.*;
import java.util.ArrayList;
import chapter9.book;
import static java.lang.System.out;
@WebServlet(name = "FuzzyLookupServlet", urlPatterns = {"/fuzzyLookupServlet"})
public class FuzzyLookupServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理响应乱码
response.setContentType("text/html;charset=utf-8");
// 处理请求乱码
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
if (name != null) {
try {
// 1、导入驱动jar包
// 2、注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 3、获取数据库的连接对象
Connection conn = DriverManager.getConnection("你的数据库", "用户名", "你的密码");
// 4、定义sql语句
String sql = "select * from book where name like '%" + name + "%'";
// 5、获取执行sql语句的对象
PreparedStatement ps = conn.prepareStatement(sql);
// 6、执行sql并接收返回结果
ResultSet rs = ps.executeQuery(sql);
// 7、处理结果
ArrayList<book> res = new ArrayList<book>();
while (rs.next()) {
res.add(new book(rs.getString(1), rs.getString(2), rs.getDouble(3)));
}
HttpSession session = request.getSession();
session.setAttribute("result", res);
// 8、释放资源
ps.close();
conn.close();
// 9、跳转
request.getRequestDispatcher("/chapter9/9_1/search.jsp").forward(request, response);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
book.java
package chapter9;
public class book {
public String id;
public String name;
public double price;
public book(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}