Ajax实现类似google搜索下拉框提示功能(4/4)

前端之家收集整理的这篇文章主要介绍了Ajax实现类似google搜索下拉框提示功能(4/4)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面的例子将为您演示当用户在输入框中键入字符时,网页如何与 web 服务器进行通信。

本示例包括2个文件。分别为showhint.html和ShowHintServlet。

以下是详细代码

showhint.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>showhint.html</title>

  5. <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <Meta http-equiv="description" content="this is my page">
  7. <Meta http-equiv="content-type" content="text/html; charset=UTF-8">

  8. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  9. <script language="javascript" type="text/javascript">
  10. var xmlHttp;
  11. function createXMLHttpRequest(){
  12. if(window.ActiveXObject){
  13. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  14. }else if(window.XMLHttpRequest){
  15. xmlHttp = new XMLHttpRequest();
  16. }
  17. }

  18. function doRequest(){
  19. createXMLHttpRequest();
  20. var url = "servlet/ShowHintServlet?q=" + document.getElementById("email").value + "&timeStamp=" + new Date().getTime();
  21. xmlHttp.onreadystatechange = handleStateChange;
  22. xmlHttp.open("GET",url,true);
  23. xmlHttp.send(null);

  24. }

  25. function handleStateChange(){
  26. if(xmlHttp.readyState == 4){
  27. if(xmlHttp.status == 200){
  28. document.getElementById("hintBox").innerHTML = xmlHttp.responseText;
  29. }
  30. }
  31. }
  32. </script>
  33. </head>

  34. <body>
  35. 请输入你的手机号码:
  36. <input type="text" name="email" id="email" onkeyup="doRequest()" />
  37. <div id="hintBox"></div>
  38. </body>
  39. </html>

ShowHintServlet代码

  1. package ajax.study;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;

  8. public class ShowHintServlet extends HttpServlet {

  9. public void doGet(HttpServletRequest request, HttpServletResponse response)
  10. throws ServletException, IOException {

  11. response.setContentType("text/html");
  12. String[] emails = {
  13. "18789245070",
  14. "13647579626",
  15. "13648649621",
  16. "18789273423",
  17. "13876221345",
  18. "13697542356",
  19. "15248939447"

  20. };
  21. PrintWriter out = response.getWriter();
  22. String q = request.getParameter("q");
  23. String hint = "";
  24. if(q.length()>0){
  25. for(int i=0; i<emails.length; i++){
  26. if(q.equalsIgnoreCase(emails[i].substring(0, q.length()))){
  27. if(hint==""){
  28. hint = emails[i];
  29. }else{
  30. hint = hint + " " + emails[i];
  31. }

  32. }
  33. }
  34. }
  35. if(hint==""){
  36. hint = "no suggestion";
  37. }
  38. out.println(hint);
  39. out.flush();
  40. out.close();
  41. }


  42. public void doPost(HttpServletRequest request, IOException {
  43. doGet(request,response);
  44. }

  45. }
原文链接:https://www.f2er.com/ajax/166288.html

猜你在找的Ajax相关文章