0

I'm working on this problem for hours but I can't find a solution. I think it's just a syntax error.

In my simple project I just want to send a JSON string to the Servlet with a name and a text in it. The servlet should take it, parse it with the org.json lib and print the name and text out. After that send it back to that Java Script, also as a JSON String.

I have tried multiple syntax versions I found on the internet but nothing works correct.

Here are the main parts of the project:

JS:

function sendMessage(event){

request = new XMLHttpRequest();

request.onreadystatechange = receivedData

var chat_text = document.getElementById("textarea_input").value;

request.open("GET", "AjaxChatServlet", true)

var jsonData;

jsonData = JSON.stringify({ username : loginname, text : chat_text});

request.setRequestHeader("Content-type", "application/json");
request.setRequestHeader("Content-length", jsonData.length);

request.send(jsonData)
document.getElementById("textarea_input").value = "";
}

Servlet:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    JSONObject jobj_username = null;
    try {
        jobj_username = new JSONObject(request.getParameter("username"));
    } catch (JSONException e) {
        // TODO Automatisch generierter Erfassungsblock
        e.printStackTrace();
    }

    JSONObject jobj_text = null;
    try {
        jobj_username = new JSONObject(request.getParameter("text"));
    } catch (JSONException e) {
        // TODO Automatisch generierter Erfassungsblock
        e.printStackTrace();
    }




    System.out.println("\n\n\nMessage send from:\n" + jobj_username + "\n\n" + "Message content:\n" + jobj_text);
    PrintWriter writer = response.getWriter();

    if(jobj_username != null && jobj_text != null){
        //send JSON back to JavaScript
    }else{
        {writer.print("no Data available");}
    }

}

Hope someone could help me. Thanks

WeSt
  • 889
  • 5
  • 14
  • 32
  • I'd encourage you to look at what is being sent and to make sure that the parameters are on the URL as your Java code is expecting. I don't think that they are. Use a tools like Firebug for Firefox or the built in Chrome developer tools to see what the URL looks like for the GET. Traditionally, it is easier to send a JSON body in a POST but that is a different discussion. – stdunbar May 18 '16 at 16:12
  • First I tried it with POST but I don't know how to get it run with that body-part. That servlet crash when I send someting to it. There must be a problem on creating the first JSONObject. `jobj_username = new JSONObject(request.getParameter("username"));` – WeSt May 18 '16 at 17:11
  • That's because it isn't sending that parameter - getParameter() assumes that it's on the URL and it isn't so you're passing null to JSONObject. What does the GET look like? – stdunbar May 18 '16 at 17:20
  • The GET URL is without any parameter, but why? `http://localhost:8080/JSON_Chat_Servlet/AjaxChatServlet` – WeSt May 18 '16 at 17:59
  • Carefully read "Manually sending request parameters to servlet" section of the duplicate. You'll quickly realize that you're mixing 2 distinct ways of sending data to servlet: as normal URL encoded request parameters, or as a whole JSON string. – BalusC May 18 '16 at 18:29
  • thanks for your help. the mixing happened because I saw to many different ways to do this.I saw that other question also before, but they use jQuery for the JS part. I don't worked with jquery till now. I will do this in the future but now I have to do it just with regular Java Script. So it's confusing – WeSt May 18 '16 at 18:37

0 Answers0