Ajax站内容搜索根据输入内容提示商品信息

动态效果:

Ajax站内容搜索根据输入内容提示商品信息_第1张图片

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Ajax站内搜索提示





	

java:

package ajax;

import java.io.IOException;
import java.security.interfaces.RSAKey;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import com.google.gson.Gson;

import utils.DataSourceUtils;

public class SearchWord extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取用户输入内容
		String word = request.getParameter("word");
		//2.根据输入内容模糊查询商品
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from product where pname like ? limit 0,8";
		List products=new ArrayList();
		try {
			products = runner.query(sql, new ColumnListHandler("pname"), "%"+word+"%");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//3.将查询结果集转换成json格式并返回给前台
		Gson gson=new Gson();
		String json = gson.toJson(products);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(json);;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
} 
  

你可能感兴趣的:(Ajax)