而无需刷新使用Ajax网页

而无需刷新使用Ajax网页

本文介绍了如何显示从数据库到JSP的价值,而无需刷新使用Ajax网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Ajax新鲜

阿贾克斯

function ajaxFunction() {
  if(xmlhttp) {
   var txtname = document.getElementById("txtname");
    xmlhttp.open("POST","Namelist",true);

    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("txtname=" + txtname.value);
  }
}

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText;
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

的jsp

<form name="fname" action="Namellist" method="post">

    Select Category :
    <select name="txtname" id="txtname">
     <option value="Hindu">Hindu</option>
     <option value="Muslim">Muslim</option>
     <option value="Christian">Christian</option>
    </select>
    <input type="button" value="Show" id="sh" onclick="ajaxFunction();">
    <div id="message">here i want to display name</div><div id="message1">here i want to display meaning</div>
    </form>

的servlet

String ct=null;
ct=request.getParameter("txtname");
      Connection con=null;
      ResultSet rs=null;
      Statement st=null;
try{
con=Dbconnection.getConnection();
PreparedStatement ps=con.prepareStatement("select name meaning from (select * from namelist order by dbms_random.value)where rownum<=20 and category='+ct+'" );
rs=ps.executeQuery();

 out.println("name" + rs);
 **Here I have confusion,**

}
catch(Exception e)
{
    System.out.println(e);
}

我怎样才能diaplay servlet的价值JSP中。请帮我?还是请大家提供一些很好的教程链接。

How can i diaplay servlet value to jsp.Please help me? or please provide some good tutorial links.

推荐答案

您必须进行以下修改: -在Servlet的: -设置响应内容类型为: - response.setContentType(为text / xml); 在servlet的顶部。通过设置这一点,我们可以把XML格式的响应,并在获取它的JSP,我们会得到它基于XML的标记名称。

You have to make below changes :-In Servlet :-Set the response content type as:- response.setContentType("text/xml"); in top section of the servlet. By setting this we can send the response in XML format and while retrieving it on JSP we will get it based on tag name of the XML.

做任何操作,你要在servlet的...保存价值为前

Do whatever operation you want in servlet...Save the value for ex-

String uname=";
     uname="hello"; //some operation
    //create one XML string
    String sendThis="<?xml version='1.0'?>"
            +"<Maintag>"
            +"<Subtag>"
            +"<unameVal>"+uname+"</unameVal>"
            +"</Subtag>"
            +"</Maintag>"
  out.print(sendThis);

现在我们将去JSP页面,我们已经来显示数据。

Now we'll go to JSP page where we've to display data.

    function getXMLObject()  //XML OBJECT
        {
            var xmlHttp = false;
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
            }
            catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
                }
                catch (e2) {
                    xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
                }
            }
            if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
            }
            return xmlHttp;  // Mandatory Statement returning the ajax object created
        }
    var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object
        function ajaxFunction() {
            if(xmlhttp) {
                xmlhttp.open("GET","NameList",true); //NameList will be the servlet name
                xmlhttp.onreadystatechange  = handleServerResponse;

                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send(null);
            }
        }
        function handleServerResponse() {
            if (xmlhttp.readyState == 4) {
                if(xmlhttp.status == 200) {
                   getVal();
                }
                else {
                    alert("Error during AJAX call. Please try again");
                }
            }
        }
       function getVal()
        {
             var xmlResp=xmlhttp.responseText;
             try{

                if(xmlResp.search("Maintag")>0 )
                {
               var x=xmlhttp.responseXML.documentElement.getElementsByTagName("Subtag");
                    var xx=x[0].getElementsByTagName("unameVal");
                    var recievedUname=xx[0].firstChild.nodeValue;
                   document.getElementById("message").innerText=recievedUname;//here
                }
                }catch(err2){
                    alert("Error in getting data"+err2);
                }
        }

在这里,你做。 :)

And here you are done. :)

这篇关于如何显示从数据库到JSP的价值,而无需刷新使用Ajax网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:13