Servlet+Ajax实现简单的搜索框智能提示


数据库连接信息
driver=com.mysql.jdbc.Driver //数据库连接驱动
url=jdbc:mysql://127.0.0.1:3306/searchofkeyword?useUnicode=true&characterEncoding=utf-8 //连接url
user=root //账户名
password=root //密码
 
  

mybatis配置信息







	
	
	
	  
	
	 	
	
	
	
		
			
			
			
			
				
				
				
				
			
		
	
	
	
	
		
	

Dao层

package cn.dao;

import java.util.List;

import cn.pojo.search;

public interface ISearchDao {
	/**
	 * 查询全部
	 * @param name
	 * @return
	 */
	List getList();
}
 
  

Mapper映射文件



	


pojo

package cn.pojo;

public class search {
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

service及实现类

package cn.service;

import java.util.List;

import cn.pojo.search;

public interface ISearchService {
	/**
	 * 查询全部
	 * @param name
	 * @return
	 */
	List getList();
}
 
  
package cn.service.impl;

import java.util.List;

import cn.dao.ISearchDao;
import cn.pojo.search;
import cn.service.ISearchService;
import cn.utils.MyBatisUtil;

public class ISearchServiceImpl implements ISearchService {
	
	@Override
	public List getList() {
		List list = MyBatisUtil.getSqlSession().getMapper(ISearchDao.class).getList();
		return list;
	}

}

servlet

package cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
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 cn.pojo.search;
import cn.service.impl.ISearchServiceImpl;

import net.sf.json.JSONArray;

public class SearchServlet extends HttpServlet {
		//static List datas = new ArrayList();
		/*static{
			datas.add("ajax");
			datas.add("ajax-post");
			datas.add("beel");
			datas.add("colection");
			datas.add("cav");
			
		}*/
		private ISearchServiceImpl issi = new ISearchServiceImpl();
		List datas = issi.getList();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//1.获得前台传输的关键字
		String keyword = request.getParameter("keyword");
		//2.处理数据
		List listdata = getDatas(keyword);
		//3.返回json格式
		//System.out.println(JSONArray.fromObject(listdata).toString());
		response.getWriter().write(JSONArray.fromObject(listdata).toString());
		
		out.flush();
		out.close();
	}
		public List getDatas(String keyword) {
			List list = new ArrayList();
			for (search da : datas) {
				if (da.getName().contains(keyword)) {
					list.add(da.getName());
				}
			}
			return list;
		}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request, response);
	}

}

mybatisUtils

package cn.utils;



import java.io.IOException;  
import java.io.Reader;  
import org.apache.ibatis.io.Resources;  
import org.apache.ibatis.session.SqlSession;  
import org.apache.ibatis.session.SqlSessionFactory;  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  
/** 
 * 工具类 
 */  
public class MyBatisUtil {  
    private static ThreadLocal threadLocal = new ThreadLocal();  
    private static SqlSessionFactory sqlSessionFactory;  
    /** 
     * 加载位于src/mybatis-config.xml配置文件 
     */  
    static{  
        try {  
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");  
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);  
        } catch (IOException e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        }  
    }  
    /** 
     * 禁止外界通过new方法创建  
     */  
    private MyBatisUtil(){}  
    /** 
     * 获取SqlSession 
     */  
    public static SqlSession getSqlSession(){  
        //从当前线程中获取SqlSession对象  
        SqlSession sqlSession = threadLocal.get();  
        //如果SqlSession对象为空  
        if(sqlSession == null){  
            //在SqlSessionFactory非空的情况下,获取SqlSession对象  
            sqlSession = sqlSessionFactory.openSession();  
            //将SqlSession对象与当前线程绑定在一起  
            threadLocal.set(sqlSession);  
        }  
        //返回SqlSession对象  
        return sqlSession;  
    }  
    /** 
     * 关闭SqlSession与当前线程分开 
     */  
    public static void closeSqlSession(){  
        //从当前线程中获取SqlSession对象  
        SqlSession sqlSession = threadLocal.get();  
        //如果SqlSession对象非空  
        if(sqlSession != null){  
            //关闭SqlSession对象  
            sqlSession.close();  
            //分开当前线程与SqlSession对象的关系,目的是让GC尽早回收  
            threadLocal.remove();  
        }  
    }
}

web.xml



  
  
    SearchServlet
    cn.servlet.SearchServlet
  

  
    SearchServlet
    /SearchServlet
  	
  
    index.jsp
  


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    
  	
  
  
  
   	
所需JAR放在图片文件

你可能感兴趣的:(servlet)