0

I am not able to send data in JSON format from my jsp to servlet controller. I think, I am somewhere wrong in my ajax call to servlet. Below is my code:

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script>
var SendInfo=[];
$(document).ready(function(){
    $("#but").click(function(){
        $(".tabclass tr td input[type='checkbox']:checked").each(function(){
            var sibs=$(this).parent().siblings("td");

            var v0= $(this).val();

            var v1=$(sibs[0]).text(); 

            var v2=$(sibs[1]).children("select[name='address']").val();

            var domain = {
                    id:v0,
                    name: v1,
                    address: v2

            }
            SendInfo.push(domain);

            alert(v0+" & "+v1+" & "+v2);

        }); 
        $.ajax({
              type: "POST",
              url: "s1.do",
              data: JSON.stringify({ students: SendInfo }), // might be wrong here or is it correct way to send data?
              success: function(data, status){
                  alert("Data: " + data + "\nStatus: " + status);
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("some error");
              }
            });
    });
});
</script>
</head>
<body>
<table border="1" class="tabclass">
<th>select</th><th>Name</th><th>Address</th>
<tr>
<td><input type="checkbox" name="selectCheck" class="select" id="ch1" value="1"/> </td>
<td><span class="name">Nitin</span></td>
<td><select name="address">
<option>Gurgaon</option>
<option>Noida</option>
<option>Rohini</option>
</select></td>
</tr>
<tr>
<td><input type="checkbox" name="selectCheck" class="select" id="ch2" value="2"/> </td>
<td><span class="name">Abc</span></td>
<td><select name="address">
<option>Gurgaon</option>
<option>Noida</option>
<option>Rohini</option>
</select></td>
</tr>
<tr>
<td><input type="checkbox" name="selectCheck" class="select" id="ch3" value="3"/> </td>
<td><span class="name">Xyz</span></td>
<td><select name="address">
<option>Gurgaon</option>
<option>Noida</option>
<option>Rohini</option>
</select></td>
</tr>
</table><br><br>
<button id="but">Test</button>
<br></br><button id="but2">Test2</button>
</body>
</html>

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String abc = request.getParameter("students");
        System.out.println("JSON >  "+abc);
    }

I am getting the output in console as :

JSON > null
JPG
  • 1,247
  • 5
  • 31
  • 64

2 Answers2

0

I just needed to change data element of ajax call to :

data: {students : JSON.stringify(SendInfo)},
JPG
  • 1,247
  • 5
  • 31
  • 64
-1

request.getParameter() reads data from the URL (query parameters) or form posted parameters (application/x-www-form-urlencoded, multipart/form-data content type).

If you post JSON you have to read it from the servlet inputStream (request.getInputStream), and parse the string.

Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • I can receive the data from request.getParameter() method if I use `$.post` way of ajax. I want to ask, why is this behavior not possible for `$.ajax` – JPG Dec 14 '16 at 08:19