簡體   English   中英

將數據從JSP發送到servlet

[英]Send data from JSP to 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);

}}

我非常感謝您的幫助!

嘗試在請求中添加標題-

這是從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);

采用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM