黑马搜索_下拉框提示

来源:互联网 发布:sql统计总金额 编辑:程序博客网 时间:2024/05/18 01:13

1.新建demo3.jsp需要导入 中写入

黑马搜索

2.创建一个Servlet名字为SearchKwServlet SearchKwServlet /searchKw 在SearchKwServlet 写入doget()方法和dopost()方法public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//0.设置编码response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");//1.获取kwString kw=request.getParameter("kw");//kw=new String(kw.getBytes("iso8859-1"),"utf-8");System.out.println(kw);//2.调用service 完成查询 返回listList list=null;try {list = new ProductService().searchKw4Ajax(kw);} catch (SQLException e) {// TODO 自动生成的 catch 块e.printStackTrace();}//3.将数据[aaa,a,aaa]去掉括号写回去a,aa,aaaif(list != null && list.size()>0){//变成字符串,不过前后有两个括号String s=list.toString();s=s.substring(1,s.length()-1);System.out.println(s);response.getWriter().println(s);}}/** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}3.新建一个ProductService类,包随意放,最好也放入service包中,建立searchKw4Ajax方法 public class ProductService {public List searchKw4Ajax(String kw) throws SQLException {// TODO 自动生成的方法存根return new ProductDao().searchByKw4Ajax(kw);}4.新建ProductDao类,包随意放,最好放入dao中,建立searchByKw4Ajax方法public class ProductDao {public List searchByKw4Ajax(String kw) throws SQLException {QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());String sql="select kw from keyword where kw like ? limit 5";return qr.query(sql, new ColumnListHandler("kw"), "%"+kw+"%");}

原创粉丝点击