• 大小: 39KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2022-01-23
  • 语言: 其他
  • 标签: Ajax项目  

资源简介

基于Servlet的Ajax百度搜索效果项目实例代码 具体的讲解请参照博文 http://blog.csdn.net/songdeitao/article/details/17998805

资源截图

代码片段和文件信息

package com.action;

import java.io.IOException;
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 strList = new ArrayList();
// StringBuffer sb = new StringBuffer();
// 获取前台输入的字符,去空格,转换成小写
String content = req.getParameter(“content“).trim().toLowerCase();
// 如果输入的字符存在且不为空
if (content != null && !““.equals(content)) {
// 将符合条件的结果放进集合中
for (String strTemp : str) {
// 以字符串开头
if (strTemp.toLowerCase().startsWith(content)) {
strList.add(strTemp);
// sb.append(strTemp);
// sb.append(“
“);
}
}
}
// if (strList.size() > 10) {
// // 结果集只保存前十个元素
// strList = strList.subList(0 10);
// }
// 存放到request属性范围中
req.setAttribute(“strList“ strList);
// 将结果在页面中输出
req.getRequestDispatcher(“WEB-INF/jsp/showAjax.jsp“).forward(req resp);
// PrintWriter print = resp.getWriter();
// print.write(sb.toString());
}

@Override
public void init() throws ServletException {
// 初始化初始数据
// 相当于数据库
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“ };
}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-01-08 14:34  Ajax百度搜索项目实例\
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\
     文件       47616  2014-01-08 14:34  Ajax百度搜索项目实例\Ajax.ppt
     文件         362  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\.classpath
     目录           0  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\.myeclipse\
     文件         285  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\.mymetadata
     文件        1410  2014-01-07 15:01  Ajax百度搜索项目实例\Ajax\.project
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\.settings\
     文件         500  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\.settings\.jsdtscope
     文件          49  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\src\
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\src\com\
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\src\com\action\
     文件        2938  2014-01-08 11:07  Ajax百度搜索项目实例\Ajax\src\com\action\AjaxAction.java
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\
     文件        1656  2014-01-08 10:37  Ajax百度搜索项目实例\Ajax\WebRoot\index.jsp
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\js\
     文件         686  2014-01-08 14:05  Ajax百度搜索项目实例\Ajax\WebRoot\js\ajax.js
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\meta-INF\
     文件          36  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\WebRoot\meta-INF\MANIFEST.MF
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\classes\
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\classes\com\
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\classes\com\action\
     文件        3983  2014-01-08 11:07  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\classes\com\action\AjaxAction.class
     目录           0  2014-01-08 14:33  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\jsp\
     文件         438  2014-01-08 10:37  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\jsp\showAjax.jsp
     目录           0  2014-01-07 13:15  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\lib\
     文件         621  2014-01-07 15:16  Ajax百度搜索项目实例\Ajax\WebRoot\WEB-INF\web.xml

评论

共有 条评论

相关资源