how to get the array in servlet which passed from ajax call

I am passing an array from servlet through ajax call. But when I am trying to get the same in servlet side, I am getting null pointer exception. I tried using one of the examples given in SO. Since I'm a newbie, any help would be appreciated!

$.ajax({
				url : 'insertserv1',
				type: 'POST',
				dataType: 'json',
				data: {tablearray:tablearray} ,
		        contentType: 'application/json',
		        mimeType: 'application/json',
				success : function(data) {
							alert('Hi');
						}			
		    });

// to get data from inspection table
		{ var tablearray = [];
			$("#tab_logic tr.data").map(function (index, tr) {
            $(this).find('td').each(function(){
                var $data = $(this).html();
                	if($(this).find("select").length > 0) {
                        var $x = $(this).find("select").val();
                    }else{
 	                	var $x = $(this).find("input[type='text']").val();
 	             	}
                	tablearray.push($x);
            });  	
         });
		}

String[] tablearray = request.getParameterValues("tablearray[]");
		
for (int i = 0; i < tablearray.length; i++) {
  System.out.println(InspTableArray[i]);
}

Answers 1

  • If I have understood correctly, this is what you would be looking for:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        JSONObject jObj = null;
        StringBuilder sb = new StringBuilder();
        BufferedReader br = request.getReader();
        String str = null;
        while ((str = br.readLine()) != null)
        {
            sb.append(str);
        }
        try
        {
            jObj = new JSONObject(sb.toString()); //This is your JSON data.
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    

Related Articles