2014-01-12 26 views
0

下面是我的JSP頁面(abc.jsp)從中我打電話另一個JSP頁面jspConnection.jsp使用jQuery,然後jspConnection.jsp將查詢結果返回我回abc.jsp,然後我需要使用的結果顯示在一個表中 -如何將JSONArray從一個JSP傳遞到另一個JSP並在表中顯示結果?

下面的代碼我有abc.jsp從中我打電話jspConnection.jsp -

$.post("jspConnection.jsp", {'id': id}, 
     function (data) { 
      // make a new table here from JSONObject 
      // and show the result here 
     } 
); 

表應該是這樣的abc.jsp迭代的JSONObject後 -

FirstName   LastName    Address    Email     PhoneNumber 
SomeValue   SomeOtherValue   SomeOtherValue  SomeOtherValue   SomeOtherValue 
...     ...      ...     ....     .... 
...     ...      ...     ....     .... 

現在下面是jspConnection.jsp,我將我的sql查詢的結果返回到abc.jsp頁面。我的SQL查詢將返回多行。

下面是我執行我的SQL查詢 -

SELECT FirstName, LastName, Libs, Email, PhoneNumber from Testing; 

現在我需要回到我上面的SELECT查詢的JSON對象 -

JSONObject obj = new JSONObject(); 
JSONArray list = new JSONArray(); 

while (resultSet.next()) { 

// This might be wrong way to make an object for my scenario 
    obj.put("FirstName", rs.getString(1)) ; 
    obj.put("LastName", rs.getString(2)) ; 
    obj.put("Address", rs.getString(3)) ; 
    obj.put("Email", rs.getString(4)) ; 
    obj.put("PhoneNumber", rs.getString(5)) ; 
} 
list.add(obj); 

response.getWriter().write(obj.toString()); 

現在我不知道怎麼回JSONObject使我可以正確地在abc.jsp中創建表。由於目前我正在使JSONObject和JSONArray的方式不正確,所以我想不能正確理解如何做到這一點?

回答

0

您需要在每次迭代開始時創建一個新的JSONObject,並且您必須在每次迭代結束時將其添加到JSONArray。也許這樣,

JSONArray list = new JSONArray(); 

while (resultSet.next()) { 
    JSONObject obj = new JSONObject();  // Move this here. 
    // This might be wrong way to make an object for my scenario 
    obj.put("FirstName", rs.getString(1)); 
    obj.put("LastName", rs.getString(2)); 
    obj.put("Address", rs.getString(3)); 
    obj.put("Email", rs.getString(4)); 
    obj.put("PhoneNumber", rs.getString(5)); 
    list.add(obj);       // And this here. 
} 
+0

感謝Elliott的建議。任何想法如何將這個JSONArray發送到我的'abc.jsp'然後生成相應的表? – AKIWEB

+0

你可能不應該這樣做。將JSONArray返回給客戶端,並使用jQuery在瀏覽器中呈現它。 –

+0

你在說什麼客戶?在我的場景中,客戶端是調用jspConnection.jsp的abc.jsp,並且此jspConnection.jsp運行select查詢並將數據返回給abc.jsp,以便可以在abc.jsp上顯示結果 – AKIWEB

相關問題