Skip to content Skip to sidebar Skip to footer

How Can I Send A Request From Javascript To A Servlet?

I have a .jsp file with JavaScript. If I click to the OK button, I call a JavaScript method. This method detects an id. I want to send this id to my servlet. In my servlet I want

Solution 1:

Use AJAX as to call servlet. Get the response back from servlet.

var xmlHttpReq = false;


if (window.XMLHttpRequest) {
   xmlHttpReq = newXMLHttpRequest();
}

elseif (window.ActiveXObject) {
    xmlHttpReq = newActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('POST', strURL, true);
xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 xmlHttpReq.onreadystatechange = function() {
    if (xmlHttpReq.readyState == 4) {
        alert(xmlHttpReq.responseText)
    }
}
xmlHttpReq.send();

Solution 2:

Use Jquery ajax for this purpose, this is simple and handy. All you need to do is use jquery plugin.

functionremoveLink(){   
     $.ajax({
     url: "<%=request.getContextPath()%>/issues?action=uploaddeletelink&wherestatement="+id,              
     type: "POST", 
     success: function(data){
      //If you want to return anything in jsp.
       } 
     });
 }

Hope this helps..

Post a Comment for "How Can I Send A Request From Javascript To A Servlet?"