0

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

  1. why the parameters are not visible when the contentType is application/json ?
  2. how to access the parameters from request when the contentType is application/json.
R.G
  • 6,436
  • 3
  • 19
  • 28
  • 1
    See this answer. maybe a duplicate http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data – pdem Oct 29 '15 at 09:45
  • Thank you . That post appears to answer my question. Will try out and close this question in few minutes – R.G Oct 29 '15 at 09:56
  • That answers my question pdem. I am not sure to keep this question or to delete it. – R.G Oct 29 '15 at 10:10
  • Keep it, it may be usefull for indexing. Someone may ask the same question – pdem Oct 29 '15 at 10:14
  • May be you can answer this and I can accept the same ? On a quick look this question appears to be not answered – R.G Oct 29 '15 at 10:45

1 Answers1

1

The parameters cannot be used in this case because the post data is not from an HTML Form. Instead you can parse the Json contained in form Data, for example using Jackson :

protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
PrintWriter out = response.getWriter();

ObjectMapper mapper = new ObjectMapper();

try {
  // read from stream, convert it to generic class
  Map data = mapper.readValue(request.getReader(), Map.class);

} catch (JsonGenerationException e) {
  e.printStackTrace();
} catch (JsonMappingException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}


}

And to use Jackson in a maven project, add this dependency:

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.5.4</version>
    </dependency>

see also other answers on the same subject: HttpServletRequest get JSON POST data

Community
  • 1
  • 1
pdem
  • 3,880
  • 1
  • 24
  • 38