I am trying a simple program using jquery which makes an ajax request to a Servlet
var searchObject = new Object();
searchObject.search1='abc';
searchObject.search2='xyz';
console.log(JSON.stringify(searchObject));
$.ajax({
    url: "SearchServlet",
    type: 'post',
    data: {data:JSON.stringify(searchObject)},
    contentType: 'application/json',
    mimeType: 'application/json',
    success: function (data) {
        console.log("Posted!!");
    }
});
Following is what gets logged on console.
{"search1":"abc","search2":"xyz"}
And in SearchServlet following is the method
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getParameterMap());
        Enumeration<String> names = request.getParameterNames();
        System.out.println(names.hasMoreElements());
        while(names.hasMoreElements()){
            System.out.println(request.getParameter(names.nextElement()));
        }
    }
which prints
{}
false
when contentType specified in the ajax request is 'application/json'
and prints
{data=[Ljava.lang.String;@7eedec92}
true
{"search1":"abc","search2":"xyz"}
when contentType is commented out from the jquery ajax request code.
I would like to understand
- why the parameters are not visible when the contentType is application/json ?
- how to access the parameters from request when the contentType is application/json.
 
     
    