5-2查询问题

5-2查询问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="cn.imooc.b.*,cn.imooc.b.*,java.util.*"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
   String basePath=request.getScheme()+"://"+request.getServerName()+":"+
                 request.getServerPort()+request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书查询</title>
<script type="text/javascript" src="<%=basePath%>/resources/js/jquery-1.4.2.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="button" 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>
            <%
            LibDaolmpl ld=new LibDaolmpl();
            List list=ld.getBooks();
            List<Book>list2=(List<Book>)this.getServletContext().getAttribute("bookList");
            if(list2==null){
            for(int i=0;i<list.size();i++){
            Book book=(Book)list.get(i);
            %>
            <tbody id="cont">
             
                    <tr>
                        <td><%=book.getId() %></td>
                        <td><%=book.getName() %></td>
                        <td><%=book.getClassName() %></td>
                        <td><%=book.getPrice() %></td>
                        <td><%=book.getDescription() %></td>
                    </tr>
                 
            </tbody>
            <%} 
            }else{
                for(int i=0;i<list2.size();i++){
                    Book book=(Book)list2.get(i);
            %>
            <tbody id="cont">
                 
                    <tr>
                        <td><%=book.getId() %></td>
                        <td><%=book.getName() %></td>
                        <td><%=book.getClassName() %></td>
                        <td><%=book.getPrice() %></td>
                        <td><%=book.getDescription() %></td>
                    </tr>
                 
            </tbody>
            <%
                }
                }
            %>
        </table>
    </center>
</body>
<script type="text/javascript">
  $("#search").click(function(){
      $.ajax({
         url:"<%=basePath%>/SearchServlet",
         type:"post",
         data:{
             bookID:$("input[name=bookID]").val(),
             bookName:$("input[name=bookName]").val(),
             catgoryName:$("input[name=catgoryName]").val()
         },
         dataType:"json",
         success:function(result){
              
         }
      });
  })
</script>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cn.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.JSONObject;
 
import cn.imooc.b.Book;
import cn.imooc.b.LibDaolmpl;
 
public class SearchServlet extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookID=request.getParameter("bookID");
        String bookName=request.getParameter("bookName");
        String catgoryName=request.getParameter("catgoryName");
        LibDaolmpl ld=new LibDaolmpl();
        List<Book>list=ld.getBooksByCondition(bookID, bookName, catgoryName);
        this.getServletContext().setAttribute("bookList", list);
        request.getRequestDispatcher(request.getContextPath()+"/showBooks.jsp");
        System.out.println("Search"+bookID);
        System.out.println("search"+list.toString());
//      JSONObject jsonObject=new JSONObject("{flag:false}");
//      response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
    }
 
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
 
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
chrismorgen 2018-06-26 17:46:11

查询的时候应使用Ajax进行局部刷新,你的逻辑有问题,正常的逻辑是使用js将查询到数据展示出来,提交的查询信息返回的数据是在Ajax中,你应从Ajax中来展示查询到的信息,你可以参考下图代码进行修改,祝学习愉快~

http://img1.sycdn.imooc.com//climg/5b320a230001a91a10990459.jpg


chrismorgen 2018-06-25 11:20:41

同学想要描述什么问题呢,可否形容一下?建议你将相关的代码贴全,方便我们帮助你准确的分析问题。祝学习愉快~

  • 提问者 慕的地1361556 #1
    查询总是出现所有的图书,要怎么改呢?hsp页面中servletContext接收不到数据
    2018-06-26 12:55:04
  • 提问者 慕的地1361556 #2
    jsp页面
    2018-06-26 12:55:27
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码