5-2查询问题
<%@ 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>
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); } }
29
收起
正在回答 回答被采纳积分+1
2回答
chrismorgen
2018-06-26 17:46:11
查询的时候应使用Ajax进行局部刷新,你的逻辑有问题,正常的逻辑是使用js将查询到数据展示出来,提交的查询信息返回的数据是在Ajax中,你应从Ajax中来展示查询到的信息,你可以参考下图代码进行修改,祝学习愉快~
chrismorgen
2018-06-25 11:20:41
同学想要描述什么问题呢,可否形容一下?建议你将相关的代码贴全,方便我们帮助你准确的分析问题。祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星