图书查询的问题,不大明白图书查询该怎么写
package com.imooc.servlet;
import java.io.IOException;
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;
import org.json.JSONArray;
import com.imooc.bean.Book;
import com.imooc.db.LibDaoImpl;
/**
* 图书查询的Servlet
*/
@WebServlet("/SelectBookServlet")
public class SelectBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
String bookName=request.getParameter("bookName");
String catgoryName=request.getParameter("catgoryName");
//获取图书表集合
LibDaoImpl libDaoImpl=new LibDaoImpl();
List<Book> bookList=libDaoImpl.getBooks();
//创建JSONArray对象,将图书表集合存进去
JSONArray jsonArray=new JSONArray(bookList);
//将数组传递回界面
response.getOutputStream().write(jsonArray.toString().getBytes("UTF-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="com.imooc.bean.*,java.util.*,com.imooc.db.*"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ "/" + request.getServletContext().getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书查询</title>
<script type="text/javascript" src="resources/js/jquery-3.3.1.js"></script>
</head>
<body>
<center>
<h1>图书查询</h1>
<p>
图书ID:<input type="text" name="bookID"> 图书名:<input type="text"
name="bookName"> 分类:<input type="text" name="catgoryName">
<input type="submit" value="查询" id="search">
</p>
<hr>
<table width="800px" cellspacing="0px" cellpadding="0px" border="1px">
<thead>
<tr>
<th>图书ID</th>
<th>书名</th>
<th>分类</th>
<th>价格</th>
<th>描述</th>
</tr>
</thead>
<tbody id="cont">
<%
LibDaoImpl libDaoImpl = new LibDaoImpl();
List<Book> list = libDaoImpl.getBooks();
for (Book bookList : list) {
%>
<tr>
<td><%=bookList.getId()%></td>
<td><%=bookList.getBookName()%></td>
<td><%=bookList.getCatgoryName()%></td>
<td><%=bookList.getPrice()%></td>
<td><%=bookList.getDescription()%></td>
</tr>
<%
}
%>
</tbody>
</table>
<script type="text/javascript">
//根据条件查询
$("#search").click(function(){
$.ajax({
url:"<%=basePath%>/SelectBookServlet",
type : "post",
dataType : "json",
success : function(result) {
var bookList = eval(result);
var content = null;
for ( var i in bookList) {
var id = bookList[i].id;
var bookName = bookList[i].bookList;
var catgoryName = bookList[i].catgoryName;
content = content + "<tr><td>" + id
+ "</td><td>" + bookList
+ "</td><td>" + catgoryName
+ "</td></tr>";
$("#cont>tr").remove();
}
$("#cont").html(content);
}
});
});
</script>
</center>
</body>
</html>
1
收起
正在回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星