搜索框输入联想功能

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;
            }
}
相关推荐
java小白小9 小时前
Guava Cache 本地缓存
java
梦梦代码精9 小时前
LikeShop 二次开发扩展能力白皮书——面向业务增长的可扩展电商架构实践
java·架构·github
极客先躯9 小时前
高级java每日一道面试题-2025年12月05日-实战篇[Dockerj]-Docker 安装后的默认存储路径是什么?如何修改?
java·docker·默认存储路径在不同系统上的区别·linux overlay2·修改存储路径的理论方法·修改流程中的关键理论点
祁_z9 小时前
LangSmith 实操指南「Agent 可观测性系统」
java·服务器
小雅痞9 小时前
[Java][Leetcode hard] 76. 最小覆盖子串
java·算法·leetcode
MegaDataFlowers9 小时前
代码自动生成
java
dllxhcjla9 小时前
Spring全套
java·后端·spring
@杰克成10 小时前
Java学习24
java·学习·idea
挨踢ren10 小时前
C++虚函数:从基础到高阶
java·开发语言·jvm
IT 行者10 小时前
Spring AI 2.0.0-M5 发布:全面转向 OpenAI Java SDK
java·人工智能·spring