搜索框输入联想功能

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 分钟前
IDEA怎么汉化&idea中文改回英文版
java·intellij-idea·idea汉化·idea怎么汉化·idea转回英文
信徒_1 小时前
SpringBoot 自动装配流程
java·spring boot·后端
小薛博客2 小时前
4、前后端联调文生文、文生图事件
java·ai
愛~杦辷个訾3 小时前
芋道项目,商城模块数据表结构
java·sql·芋道·yudao-cloud·芋道商城
C_Liu_7 小时前
C语言:深入理解指针(5)
java·c语言·算法
佛祖保佑永不宕机7 小时前
麒麟系统ARM64架构部署mysql、jdk和java项目
java·arm
qqxhb8 小时前
零基础学Java——第十一章:实战项目 - 桌面应用开发(JavaFX入门)
java·开发语言·javafx
hy.z_7778 小时前
【数据结构】链表 LinkedList
java·数据结构·链表
Akiiiira9 小时前
【数据结构】队列
java·开发语言·数据结构
程序媛学姐9 小时前
Java级联操作:CascadeType的选择与最佳实践
java·开发语言