搜索框输入联想功能

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.7.0.js"></script>
    <script>
        $(function () {
            $("#search").bind("input propertychange", function () {
                //获取输入框输入的值
                var name = $(this).val();
                //判断获取的值是否为空
                if (name != null && name != "") {
                    //清理
                    $("#show").html("");
                    //从后台获取数据
                    $.get("SearchServlet", {"name": name}, function (date) {
                        //判断是否为空
                        if (date != name && date != "") {
                            for (var i = 0; i < date.length; i++) {
                                $("#show").append("<div>"+date[i]+"</div>")
                            }
                        }
                    }, "json");
                }else {
                    //清理
                    $("#show").html("");
                }
            })
        });
    </script>
</head>
<body>
<div id="box">
    <input id="search" type="text" name="search">
    <button id="btn">百度一下</button>
</div>
<div id="show">

</div>
</body>
</html>

后台

java 复制代码
package com.etime.servlet;

import com.etime.dao.StudentDao;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "SearchServlet", value = "/SearchServlet")
public class SearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        StudentDao studentDao = new StudentDao();
        String name = request.getParameter("name");
        List studentByName = studentDao.getStudentByName(name);
        if (studentByName != null) {
            ObjectMapper mapper = new ObjectMapper();
            String res = mapper.writeValueAsString(studentByName);
            PrintWriter writer = response.getWriter();
            writer.print(res);
            writer.close();
        }
    }
}

dao

java 复制代码
package com.etime.dao;

import com.etime.QueryRunnerUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import java.sql.SQLException;
import java.util.List;

public class StudentDao {
            QueryRunner queryRunnerUtils = QueryRunnerUtils.getQueryRunner();
            public List getStudentByName(String name){
                String sql = "select sname from tb_student where sname like ?";
                List sname = null;
                try {
                    sname = queryRunnerUtils.query(sql, new ColumnListHandler("sname"), "%" + name + "%");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return sname;
            }
}
相关推荐
7***374537 分钟前
Java设计模式之工厂
java·开发语言·设计模式
程序员小白条1 小时前
你面试时吹过最大的牛是什么?
java·开发语言·数据库·阿里云·面试·职场和发展·毕设
折翅嘀皇虫1 小时前
fastdds.type_propagation 详解
java·服务器·前端
小年糕是糕手1 小时前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
豐儀麟阁贵1 小时前
8.2异常的抛出与捕捉
java·开发语言·python
老华带你飞1 小时前
社区养老保障|智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·社区养老保障
码龄3年 审核中1 小时前
Linux record 03
java·linux·运维
q***87601 小时前
springboot下使用druid-spring-boot-starter
java·spring boot·后端
程序员西西1 小时前
SpringBoot无感刷新Token实战指南
java·开发语言·前端·后端·计算机·程序员