1

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?

Sambuddha
  • 37
  • 1
  • 6

3 Answers3

1

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

Community
  • 1
  • 1
psi
  • 269
  • 2
  • 13
0

Check following lines you'll identify the mistake.

var params = "parm1=" + value1 + "&parm2=" + value2;

in javascript

String sParam1 = request.getParameter("param1");
String sParam2 = request.getParameter("param2");

in your servlet

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
  • The code was simply an illustrative one. I didnt use the actual var names I am using in the real code. `parm` vs `param` was a mere oversight if you meant that. Corrected it nevertheless. **N.B.** Its not a data issue for sure, as its only happening sometimes even with the same data – Sambuddha Jul 09 '14 at 08:24
  • I hope there is no type miss match while you are catching your variables. – Darshan Lila Jul 09 '14 at 09:13
0

I suggest you can use firebug firefox plugin to monitor the every XHR call in your web page, and check the exact value of the XHR post request send, also the request header and some other options, base on your scenario, if I was the developer, I will try the post the parm1 and parm2 value to servlet by native web form first, if there has no null value in logic flow, then switch to XHR call. BTW I will recommend you to use jQuery to send XHR request, it's easier, check the doc: http://api.jquery.com/jquery.ajax/

KingQQ
  • 173
  • 2