2014-07-02 154 views
1

這裏是我的需求:從JSP將數據發送到servlet中

在JSP我填一個「表」使用JavaScript(我的第一個「TR」爲標題,第二個「TR」是輸入,則所有新「tr」添加了一個javascript函數)

現在我想將我的表的值發送到一個servlet。 我不使用jQuery框架

這裏是我一直在做的JS:

function test(){ 
    var strPar= "Blabla BLABLA BLA"; 
    var toServer = JSONObject.toJSONString(strPar); 
    alert("TEST U MF !!!"); 
    var request = new XMLHttpRequest(); 
    request.open("POST", "http://localhost:8084/Calendar/Legal", true); 
    request.send(toServer); 
    alert("POUET !"); 
} 

我似乎並沒有在所有的工作!此外,我不知道如何從servlet獲取JSON對象?

public class LegalCalServlet extends HttpServlet { 

/** 
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
* methods. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    System.out.println("TEST "); 
    String output = request.getParameter("strPar"); 
    System.out.println(output); 

}} 

我非常感謝這方面的任何幫助!

+0

第一步是檢查是否是客戶端或服務器錯誤:瀏覽器控制檯中是否有任何錯誤?順便說一句:你應該用'console.log(...)'替換'alert(...)'並檢查控制檯,而不是彈出 –

+0

抱歉,沒有看到你的通訊。 你對我的警報()很正確,但我用它作爲smtg其他人,我是一個非常懶惰的人:複製/粘貼是我的東西!哈哈 – Kermit

回答

0

用途:

xmlhttp.open("POST","demo_post2.asp",true); 
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
xmlhttp.send("fname=Henry&lname=Ford"); 

您可以在此網站上查詢的例子和教程:http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

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) 
    { 
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("POST","demo_post2.asp",true); 
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
xmlhttp.send("fname=Henry&lname=Ford"); 
} 
+0

YEAAAAAAAAAAH! Thx隊友!我實際上並沒有實例化XMLHttpRequest()!這就是爲什麼。 只需要現在處理它的servlet端,我很好:) – Kermit

+0

簡單:request.getParameter(「lname」); THX DUDE:) – Kermit

+0

@Kermit如果此答案符合您的需求,請將其標記爲「所選答案」,請:) :) –

0

嘗試增加在請求頭 -

這裏是發佈數據的一個例子http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

var url = "get_data.php"; 
var params = "lorem=ipsum&name=binny"; 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 

http.onreadystatechange = function() {//Call a function when the state changes. 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
} 
http.send(params); 
+0

是的,我使用「GET」方法,但它沒有工作。 我只是試過你的代碼,它不工作... – Kermit

+0

然後嘗試使用Ajax - http://www.openjs.com/articles/ajax_xmlhttp_using_post.php –

+0

好吧,我只是複製/粘貼你的代碼,並在我的測試項目適應名稱它只是不工作!它在「http.open(」POST「,url,true)後停止;」 – Kermit