Sambuddha Sambuddha - 5 months ago 25
Java Question

Servlet gets blank data from Javascript XHR occasionally

I am sending some parameters to a servlet from Javascript. Occasionally the servlet is receiving a set of null parameters (checked through

System.out.println()
right at the beginning of the servlet code).

However the parameters are being generated correctly in the Javascript right before the call to XHR.send (checked through
console.log()
)

Additional Background:

The servlet generates a spreadsheet from some data files, so it can be bit intensive at times.

Sample Code


web.xml:

<servlet>
<description>Export Servlet</description>
<display-name>ExportServlet</display-name>
<servlet-name>ExportServlet</servlet-name>
<servlet-class>testpkg.ExportServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExportServlet</servlet-name>
<url-pattern>/ExportServlet</url-pattern>
</servlet-mapping>


JAVASCRIPT: common.js:
function sendDataToServer(args...)
{

// ... Some Code...
var params = "param1=" + value1 + "&param2=" + value2;
console.log(params); // This works perfectly!

// AJAX Code
var xmlhttp;
// Handling browsers
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)
{
// MARKER 1.0
alert("Success");
}
};
xmlhttp.open('POST', 'ExportServlet', false);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8');
console.log(params); // This works perfectly!
// Sending data...
xmlhttp.send(params);
}


SERVLET: ExportServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Init Code...
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
request.setCharacterEncoding("UTF-8");
try
{
// Get paramaters
String sParam1 = request.getParameter("param1");
String sParam2 = request.getParameter("param2");
// Test print params...
System.out.println("Param1 Value = " + sParam1); // Occasionally NULL
System.out.println("Param2 Value = " + sParam2); // Occasionally NULL

// More Code...
}
catch (Exception ex)
{
throw ex;
}
}


N.B. The null params are not due to a data issue, as I am getting this error with the exact same data use-case which worked minutes ago. Usually this is happening randomly when the same steps are carried out repeatedly.

Any idea why this may be happening?

psi psi
Answer

try

xmlhttp.open("POST","ExportServlet?param1=" + value1 + "&param2=" + value2,true);
xmlhttp.send();

instead of

xmlhttp.open('POST', 'ExportServlet', false);
xmlhttp.send(params);

OR YOU CAN USE JQUERY AJAX

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
function sendDataToServerWithJquery(value1,value2) {
    var params ={"param1":value1,"param2":value2};
    /*
    OR
    var params = "param1=" + value1 + "&param2=" + value2;
    */

             $.ajax({type: "POST",
            url:"ExportServlet",
            data:params,
            success:function(result){
            alert("Success");
            }
          });
}
</script>
<form action="ExportServlet">

<input type="button" onclick="sendDataToServerWithJquery('P1','P2')" value="Submit Values">

</form>

Note : Use input type="button" instead of the input type="submit" Refer