2012-07-03 31 views
4

刷新頁面,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中。 請幫幫我嗎?或者請提供一些很好的教程鏈接。

+0

這是毫無疑問的可能,但它可以通過AJAX增加定期輪詢服務器的負載。 – Ars

+0

什麼是名單?它是web.xml中servlet的名稱嗎? – MaVRoSCy

+0

@MaVRoSCy:肯定的servlet,名稱列表是servlet的名稱。 – Prashobh

回答

4

您必須作出以下更改: - 在Servlet中: - 將響應內容類型設置爲: - response.setContentType("text/xml");位於servlet的頂部部分。通過設置這個,我們可以發送XML格式的響應,並在JSP上檢索它時,我們將根據XML的標籤名稱獲取它。

你的servlet中想要的任何操作... 另存爲前

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頁面的價值。

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); 
       } 
     } 

在這裏你完成了。 :)

+1

並且爲了刷新數據即從數據庫獲取更新的數據,您可以通過setTimeout(ajaxFunction,2000)來設置超時時間;在getVal()方法結束時,即每2秒。這將在每2秒鐘後調用一次servlet,併爲您提供更新的數據。 – Ars

2

1.In servlet代碼

PrintWriter output = response.getWriter(); 
String result = "value"; 
writer.write(result); 
writer.close() 

2.爲什麼你不使用jQuery的?
您可以更換您的代碼 -

$.post('url', function(data) { 
$('#message1').html(data); 
}); 

query post example

2

可能被打爆,但可能是有用的,而不是忍受所有的JavaScript的Ajax調用使用一些JavaScript庫最好的jQuery製作阿賈克斯呼叫。

你使用任何JavaScript庫將幫助您使代碼緊湊,簡潔,也將幫助你保持跨瀏覽器兼容。

如果你打算自己寫所有的XHTTP代碼,你可能最終花費了大量的時間用於固定跨瀏覽器的問題,你的代碼將有大量的黑客,而不是實際的業務邏輯。

此外,使用庫像jQuery還將與較少的代碼行的數目達到同樣的東西。

希望有所幫助。

相關問題