Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 649|回复: 0

帮我看看(使用Ajax实现自动提示功能)为啥没动静

[复制链接]

该用户从未签到

发表于 2011-10-29 09:21:29 | 显示全部楼层 |阅读模式
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<HTML>
  <head>
   
  <title>使用Ajax实现自动提示功能</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
body{font:11px arial;}
.suggest_link{
background-color:#FFFFFF;
padding:2px 6px 2px 6px;
}
.suggest_link_over{
background-color:#E8F2FE;
padding:2px 6px 2px 6px;
}
#suggest{
position:absolute;
background-color:#E82FE;
text-align:left;
border:1px solid #000000;  
display:none;
}
</style>
<script type="text/javascript">
var xmlHttp;//声明浏览器初始化对象变量
function searchSuggest(){
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
} catch (e) {
try {
xmlHttp = new XMLHttpRequest();
//针对某些特定版本的mozillar浏览器的BUG进行修正
if(xmlHttp.overrideMimeType){
//强制将MIME类型设置为text/xml
xmlHttp.overrideMimeType("text/xml");
}
} catch (e) {
// TODO: handle exception
}
}
}
//创建请求结果处理程序
xmlHttp.onreadystatechange = processRequest;
var str = document.getElementById("txtSearch").value;

if(xmlHttp!=null){
alert(str);
xmlHttp.open("GET","SearchSuggestServlet?seach="+str,true);
xmlHttp.send(null);
}else{
alert("不能创建XMLHttpRequest对象实例");
}
}
//给String对象添加去除左右空格方法
/*
String.prototype.trim = function(){
var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
return (m==null)? "":m[1];
}*/
//对返回数据进行处理
function processRequest(){
if(xmlHttp.readyState == 4){//请求响应完成
if(xmlHttp.status == 200){
//responseTest表示请求完成后,返回字符串信息
var sobj = document.getElementById("suggest");
sobj.innerHTML;
var str = xmlHttp.responseText.split("-");
var suggest = "";
if(str.length > 0 && str[0].length>0){
for(var i = 0;i<str.length;i++){
suggest += "<div onmouseover='javascript:suggestOver(this);'";
suggest += "onmouseover='javascript:suggestOver(this);'";
suggest += "onclick='javascript:setSearch(this.innerHTML);''";
suggest += "class='suggest_link'>"+str+"</div>";  
}
sobj.innerHTML=suggest;
document.getElementById("suggest").style.display="block";
}else{
document.getElementById("suggest").style.display="none";
}
}else{
alert("请求处理返回的数据有错误...");
alert(xmlHttp.status);
}
}
}
function suggestOver(obj){
obj.className ="suggest_link_over";
}
function suggestOut(obj){
obj.className ="suggest_link";
}
</script>
  </head>
   
  <body>
  <h3>Ajax实现搜索提示</h3>
  <div style="width:500px;">
  <form action="" id="formSearch">
  <input type="text"" id="txtSearch" name="txtSearch" value="" onkeyup="searchSuggest()" autocomplete="off"/>
  <input type="submit" id="cmdSearch" name="cmdSearch" value="搜索" /><br>
  <div id="suggest" style="width:200px;"></div>
  </form>
  </div>
  </body>
</html>

package Example_Ajax_P296;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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;

public class SearchSuggestServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public SearchSuggestServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*  
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String str = new String(request.getParameter("seach").getBytes("ISO-8859-1"));
PrintWriter out = response.getWriter();

List strList = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select distinct(title) from MESSAGE where title like '%" + str + "%' order by title";

try {
if(str!=null && str.length()>0){
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=company";
conn = DriverManager.getConnection(url, "sa", "89757");
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
strList.add(rs.getString("title"));
}
}
StringBuffer sbu = new StringBuffer();
int size = strList.size();
for(int i=0;i<size;i++){
sbu.append((String)strList.get(i)+"-");
}
out.print(sbu.toString());
request.getSession().setAttribute("a", sbu.toString());
response.sendRedirect("MyJsp.jsp");
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*  
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-4-29 00:11 , Processed in 0.406446 second(s), 50 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表