weixin_33711641 2014-12-29 04:01 采纳率: 0%
浏览 31

HTML + Ajax + servlet无法正常工作

hi im trying out a servlet based ajax here in my program im using a html page with button and textbox when i click the button it should fire some ajax request to the server done some computations and return result in the same page

<html>
    <head>
        <title></title>


    </head>
    <body>
       <h3>Ajax in servlets</h3>

        <div id="change"> hi there</div>
        <input type="text" id="fname" name="solo">
        <input type="button" align="center" value="Ajax" onclick="ajaxreq()" />
    </body>
</html>

in this html page a text box and a button when i click on the button it should execute the following java script function

 var xmlreq=null;
            var a=document.getElementById("fname");
            function ajaxreq(){


                if(window.XMLHttpRequest){
                    xmlreq=new XMLHttpRequest();

                }
                    else if(window.ActiveXObject){
                        xmlreq=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    try{
                  xmlreq.onreadystatechange=response;
                     xmlreq.open("GET","Servlethello?an=+a",true);
                        xmlreq.send();

        }
                    catch(e){

                        alert("some error")
                    }
    }
function response(){
var res=xmlreq.responseText;
document.write(res);
alert(res);
document.getElementById("change").innerHTML=res;

                  }








        </script>
  here i have created a ajax call and send the request to the servlet Servlethello



public class Servlethello extends HttpServlet {
    @Override
public void doGet(HttpServletRequest req,
                     HttpServletResponse res)throws ServletException, IOException {
res.setContentType("text/html");
  PrintWriter out = res.getWriter();
out.println("<h1>");
String a=req.getParameter(an);
out.println("solorules"+a);
out.println("</h1>");
out.close();


}


    }

the variable a fetches the value entered in the textbox and send it to the servlet xmlreq.open("GET","Servlethello?an=+a",true); to this servlet

public void doGet(HttpServletRequest req,
                     HttpServletResponse res)throws ServletException, IOException {
res.setContentType("text/html");
  PrintWriter out = res.getWriter();
out.println("<h1>");
String a=req.getParameter(an);
out.println("solorules"+a);
out.println("</h1>");

and this servlet should be displayed here hi there using this java script

var res=xmlreq.responseText;
    document.write(res);
    alert(res);
    document.getElementById("change").innerHTML=res;
  • 写回答

2条回答 默认 最新

  • 零零乙 2014-12-29 04:09
    关注

    You can use jquery ajax , it very simple rather than pure JS ajax.

    See the below link http://api.jquery.com/jquery.ajax/

    Sample Code :

    $.ajax({
      url: "test.html",
      dataType:"html"
      context: document.body
    }).done(function(res) {
     alert(res);
    });
    

    With Pure JS :

    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            alert(xmlhttp.responseText);
          }
       }
    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    

    For more detail referbelow link : http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿