搜索框输入联想功能

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;
            }
}
相关推荐
考虑考虑42 分钟前
JDK9中的dropWhile
java·后端·java ee
想躺平的咸鱼干1 小时前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
hqxstudying1 小时前
java依赖注入方法
java·spring·log4j·ioc·依赖
·云扬·1 小时前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
Bug退退退1232 小时前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
小皮侠2 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
The_cute_cat3 小时前
Ajax和Axios的初步学习
前端·学习·ajax
Zz_waiting.3 小时前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象
全栈凯哥3 小时前
02.SpringBoot常用Utils工具类详解
java·spring boot·后端
兮动人3 小时前
获取终端外网IP地址
java·网络·网络协议·tcp/ip·获取终端外网ip地址