title: Send a simple JSON object in Java to a Servlet
body: <p>I'm trying to send a JSONObject (org.json) from a java client to a servlet, but in my server side i get 'null' for HttpServletRequest.getParameter("Command") or any parameter.</p>
  
  <p>In my client side:</p>
  
  <pre><code>JSONObject json = new JSONObject();
      try{
          json.put("Command","spost");
          json.put("Name","pc1");
          json.put("Pwd","pc1");
          sendRequest(json);
      } catch(JSONException jsone){
  
      }
      URL url;
      HttpURLConnection connection = null;
      ObjectOutputStream out;
      try {
          url = new URL("http://myURL.com/myservlet");     //Creating the URL.
          connection = (HttpURLConnection) url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "application/json");
          //connection.setRequestProperty("Accept", "application/json");
          connection.setUseCaches(false);
          connection.setDoInput(true);
          connection.setDoOutput(true);
          //Send request
          OutputStream os = connection.getOutputStream();
          OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
          System.out.println(json.toString());
          osw.write(json.toString());
          osw.flush();
          osw.close();
          if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
              System.out.println("Ok response");
          } else {
              System.out.println("Bad response");
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  </code></pre>
  
  <p>Then I get something like this when printing json.toString():</p>
  
  <pre><code>{"Name":"pc1","Command":"SignalPost","Pwd":"pc1"}
  </code></pre>
  
  <p>...that looks pretty normal to me.</p>
  
  <p>I see an "Ok response" and my servlet detects the httprequest, but seems there is something wrong understanding the json object</p>
  
  <p>My servlet is working fine for another client i made using AJAX, so i guess the problem is in this java client.</p>
  
  <p>Could you please help me? I googled and tried everything with no luck</p>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>At the end, in server-side, this code is working:</p>
  
  <pre><code>    @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      try{
          StringBuilder sb = new StringBuilder();
          BufferedReader br = request.getReader();
          String str = null;
          while ((str = br.readLine()) != null) {
              sb.append(str);
              System.out.println(str);
          }
          JSONObject jObj = new JSONObject(sb.toString());
          String name = jObj.getString("Name");
          String pwd = jObj.getString("Pwd");
          String command = jObj.getString("Command");
  
          JSONObject json = new JSONObject();
          response.setContentType("application/json");
          response.setHeader("Cache-Control", "nocache");
          response.setCharacterEncoding("utf-8");
          PrintWriter out = response.getWriter();
          out.print(json.toString());
      } catch (Exception e) {
        e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Although i would prefer to keep using GET request from clients so that i don't have to remake all my servlet side</p>