4

Here is my Ajax code:

    var myJSONObject = {"bindings": [
                                     {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}]
                             };
    $.ajax({
        url : "ships",
        data : myJSONObject,
        success : function(data){
            GLOBAL.player.startShooting(data);
        },
        error : function(data) {
            console.log("error:", data);
        },
        dataType : "json",
        timeout : 30000,
        type : "post"
    });

And here is my Java Servlet code:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    System.out.println("im in PSOT");
    System.out.println(request.getParameter("myJSONObject"));

    StringBuilder sb = new StringBuilder();
    BufferedReader br = request.getReader();
    String str;
    while( (str = br.readLine()) != null ){
        sb.append(str);
    }   
    System.out.println(sb.toString());
    response.setContentType("application/json");
    response.getWriter().write("{\"key\":\"hello\",\"key2\":\"world\"}");
}

The Java servlet returns my Hello World object, but i CANNOT read data in Java Servlet The console prints out the following:

im in PSOT
null

The last line is an empty string from last println.

I am using Tomcat 7

Can anyone please tell me what i am doing wrong and why i cannot read data in Java Servlet_

1
  • Try getParameterwith bindings not myJSONObject which is just a variable Commented Mar 22, 2012 at 12:35

2 Answers 2

7

The parameter name is not myJSONObject. That's the JS variable name. The parameter names are all the root keys which you have there in your JSON object. E.g.

String bindings = request.getParameter("bindings");
// ...

You'd only need to manually parse it further. You could use Google Gson for this.

As to why the Reader didn't return anything, that's because the request body can be read and parsed only once. Any getParameter() call will implicitly do that. So when you call getParameter() before getReader(), you won't be able to read the request body by the Reader (the same applies for the other way round!). But you don't need it anyway. Just use getParameter() with the proper parameter names.

Sign up to request clarification or add additional context in comments.

2 Comments

Sir, thank you very much. I can take it from here. I would upvote you 1000 times, if i could
I am doing exactly the same thing, but I get a null pointer exception, i have posted my question here : stackoverflow.com/questions/11292400/…
0

You'd only need to manually parse it further. You could use Google Gson for this.

As to why the Reader didn't return anything, that's because the request body can be read and parsed only once. Any getParameter() call will implicitly do that. So when you call getParameter() before getReader(), you won't be able to read the request body by the Reader (the same applies for the other way round!). But you don't need it anyway. Just use getParameter() with the proper parameter names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.