package com.action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 执行Ajax的action
*
* @author Steven
*
*/
public class AjaxAction extends HttpServlet {
private String[] str;
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 用来接收获取符合条件的结果
List<String> strList = new ArrayList<String>();
// StringBuffer sb = new StringBuffer();
// 获取前台输入的字符,去空格,转换成小写
String content = req.getParameter("content").trim().toLowerCase();
// 如果输入的字符存在且不为空
if (content != null && !"".equals(content)) {
// 将符合条件的结果放进集合中
for (String strTemp : str) {
// 以字符串开头
if(strTemp != null){
if (strTemp.toLowerCase().startsWith(content)) {
strList.add(strTemp);
// sb.append(strTemp);
// sb.append("<br/>");
}
}
}
}
if (strList.size() > 10) {
// 结果集只保存前十个元素
strList = strList.subList(0, 10);
}
// 存放到request属性范围中
req.setAttribute("strList", strList);
// 将结果在页面中输出
req.getRequestDispatcher("WEB-INF/jsp/showAjax.jsp").forward(req, resp);
}
@Override
public void init() throws ServletException {
// 初始化初始数据
// 相当于数据库
//test1();
try {
str = CRUD.read();
for(int i=0; i<str.length;i++)
System.out.println(str[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void test1(){
// 初始化初始数据
// 相当于数据库
str = new String[] { "about", "above", "afraid", "after", "afternoon",
"again", "age", "ago", "agree", "air", "airplane", "airport",
"all", "almost", "along", "already", "also", "always",
"American", "and", "angry", "away", "autumn", "aunt", "August",
"ask", "as", "art", "arrive", "around", "arm", "April",
"apple", "appear", "apartment", "anything", "anyone", "any",
"answer", "another", "animal", "boy", "banana", "band",
"idiom", "illuminate", "highlight", "heritage", "grief",
"grab", "glitter", "legitimate", "likelihood", "media",
"mediate", "negligible", "narrative", "offence", "offensive",
"overthrow", "periodic", "publicity", "qualification",
"qualitative", "reign", "repay", "replacement", "revolve",
"scared", "seminar", "token", "transient", "update",
"vegetation", "virgin", "zinc", "yell", "yoke", "youngster",
"whereby", "ward", "vicious", "versatile", "tuck", "trigger",
"transient", "transaction", "theme", "handbook", "grim",
"extract", "entity", "edit", "distract", "discount",
"differentiate", "destiny", "delegate", "couch" };
}
}
servlet+jsp实现百度搜索自动补全功能
需积分: 10 20 浏览量
2016-08-24
17:34:15
上传
评论
收藏 3.17MB ZIP 举报
真正的成长
- 粉丝: 2
- 资源: 6
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈