搜索框输入联想功能

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;
            }
}
相关推荐
linux-hzh1 小时前
百日算法修炼 · Day 03
java·算法
不负岁月无痕2 小时前
简单理解操作系统结构
java·linux·c语言·开发语言·c++·面试
mister_guo2 小时前
Java线程池参数应该如何设置(ThreadPoolExecutor)
java
余额瞒着我当琳2 小时前
C++模板初阶与STL初探
android·java·c++
淡海水2 小时前
15-02-YooAsset面试篇-Unity架构设计与源码
java·unity·面试·c#·游戏引擎·yooasset
weixin_BYSJ19872 小时前
springboot酒店管理系统--附源码27436
java·spring boot·python·随机森林·贪心算法·eclipse·django
2401_894915533 小时前
GEO 源码部署如何实现精准地域分发?核心配置参数深度讲解
java·运维·服务器·后端·开源
evans在进步4 小时前
LeetCode 2 两数相加:链表模拟加法,Java 图解进位过程
java·leetcode·链表
2401_858286115 小时前
OS82.【Linux】设计线程池
java·linux·运维·服务器·开发语言·算法·线程池
数据知道5 小时前
IDOR 不安全的直接对象引用:API 越权实战检测
java·开发语言·网络·安全·网络安全