Ajax实现搜索文本框自动联想功能,javassm框架面试重点
作者:互联网
后台servlet代码:
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/Searchservlet")
public class Searchservlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容类型和编码格式
request.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset=utf-8”);
response.setCharacterEncoding(“utf-8”)
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
;
//获取用户输入的内容
String name = request.getParameter(“name”);
//模拟数据库的查询操作
List list = new ArrayList<>();
list.add(“百度”);
list.add(“百事可乐”);
list.add(“百世快递”);
list.add(“百度翻译”);
list.add(“百度百科”);
list.add(“美国”);
list.add(“美元”);
list.add(“网易”);
list.add(“美团”);
list.add(“滴滴”);
list.add(“滴水”);
list.add(“小米”);
list.add(“小新”);
list.add(“OPPO”);
list.add(“三星”);
list.add(“花伤情犹在”);
list.add(“花朵”);
//定义变量将匹配结果保存起来
String info = “”;
//根据用户输入的内容语句数据库查询的结果进行比较
if (!"".equals(name)){
for (int i = 0; i < list.size(); i++) {
//toUpperCase()将字符串转大写 toLowerCase()将字符串转小写—》目的忽略搜索时的字符串大小写
if (list.get(i).indexOf(name.toUpperCase())>-1 || list.get(i).indexOf(name.toLowerCase())>-1){
if (i==list.size()-1){
info+=list.get(i);
}else {
info+=list.get(i)+",";
}
标签:name,list,add,文本框,Ajax,javassm,import,servlet,javax 来源: https://blog.csdn.net/m0_63174811/article/details/121945546