加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:02:10 所属栏目:百科 来源:网络整理
导读:下面的例子将为您演示当用户在输入框中键入字符时,网页如何与 web 服务器进行通信。 本示例包括2个文件。分别为showhint.html和ShowHintServlet。 以下是详细代码: showhint.html !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" html hea

下面的例子将为您演示当用户在输入框中键入字符时,网页如何与 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. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读