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">
    @H_404_25@
  2. <html>
    @H_404_25@
  3. <head>
    @H_404_25@
  4. <title>showhint.html</title>
    @H_404_25@

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

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

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

  27. @H_404_25@
  28. }
    @H_404_25@

  29. @H_404_25@
  30. function handleStateChange(){
    @H_404_25@
  31. if(xmlHttp.readyState == 4){
    @H_404_25@
  32. if(xmlHttp.status == 200){
    @H_404_25@
  33. document.getElementById("hintBox").innerHTML = xmlHttp.responseText;
    @H_404_25@
  34. }
    @H_404_25@
  35. }
    @H_404_25@
  36. }
    @H_404_25@
  37. </script>
    @H_404_25@
  38. </head>
    @H_404_25@

  39. @H_404_25@
  40. <body>
    @H_404_25@
  41. 请输入你的手机号码:
    @H_404_25@
  42. <input type="text" name="email" id="email" onkeyup="doRequest()" />
    @H_404_25@
  43. <div id="hintBox"></div>
    @H_404_25@
  44. </body>
    @H_404_25@
  45. </html>@H_404_25@

ShowHintServlet代码

  1. package ajax.study;
    @H_404_25@

  2. @H_404_25@
  3. import java.io.IOException;
    @H_404_25@
  4. import java.io.PrintWriter;
    @H_404_25@

  5. @H_404_25@
  6. import javax.servlet.ServletException;
    @H_404_25@
  7. import javax.servlet.http.HttpServlet;
    @H_404_25@
  8. import javax.servlet.http.HttpServletRequest;
    @H_404_25@
  9. import javax.servlet.http.HttpServletResponse;
    @H_404_25@

  10. @H_404_25@
  11. public class ShowHintServlet extends HttpServlet {
    @H_404_25@

  12. @H_404_25@
  13. public void doGet(HttpServletRequest request, HttpServletResponse response)
    @H_404_25@
  14. throws ServletException, IOException {
    @H_404_25@

  15. @H_404_25@
  16. response.setContentType("text/html");
    @H_404_25@
  17. String[] emails = {
    @H_404_25@
  18. "18789245070",
    @H_404_25@
  19. "13647579626",
    @H_404_25@
  20. "13648649621",
    @H_404_25@
  21. "18789273423",
    @H_404_25@
  22. "13876221345",
    @H_404_25@
  23. "13697542356",
    @H_404_25@
  24. "15248939447"
    @H_404_25@

  25. @H_404_25@
  26. };
    @H_404_25@
  27. PrintWriter out = response.getWriter();
    @H_404_25@
  28. String q = request.getParameter("q");
    @H_404_25@
  29. String hint = "";
    @H_404_25@
  30. if(q.length()>0){
    @H_404_25@
  31. for(int i=0; i<emails.length; i++){
    @H_404_25@
  32. if(q.equalsIgnoreCase(emails[i].substring(0, q.length()))){
    @H_404_25@
  33. if(hint==""){
    @H_404_25@
  34. hint = emails[i];
    @H_404_25@
  35. }else{
    @H_404_25@
  36. hint = hint + " " + emails[i];
    @H_404_25@
  37. }
    @H_404_25@

  38. @H_404_25@
  39. }
    @H_404_25@
  40. }
    @H_404_25@
  41. }
    @H_404_25@
  42. if(hint==""){
    @H_404_25@
  43. hint = "no suggestion";
    @H_404_25@
  44. }
    @H_404_25@
  45. out.println(hint);
    @H_404_25@
  46. out.flush();
    @H_404_25@
  47. out.close();
    @H_404_25@
  48. }
    @H_404_25@

  49. @H_404_25@

  50. @H_404_25@
  51. public void doPost(HttpServletRequest request, IOException {
    @H_404_25@
  52. doGet(request,response);
    @H_404_25@
  53. }
    @H_404_25@

  54. @H_404_25@
  55. }@H_404_25@

猜你在找的Ajax相关文章