title: how to send json object from server to jsp and receive a response on jsp
body: <p>My java server receives JSON from JSP and make some operation and I try to send JSON object to JSON in the following way:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  {
  
      JSONObject newObj = new JSONObject();
  
      try {
          newObj = new JSONObject(request.getParameter("jsonData"));
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      int  val2 = 0;
      int val1 = 0;
  
      try {
          val2 = Integer.parseInt(newObj.getJSONObject("mydata").getString("number2"));
          val1 = Integer.parseInt(newObj.getJSONObject("mydata").getString("number1"));
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      System.out.println(val2 + val1);
      Double d =  Math.pow(val1, 2);
  
      PrintWriter writer = response.getWriter();
      JSONObject obj=new JSONObject();
  
      try {
          obj.put("results",d);
          obj.put("resultText","foo");
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      writer.println(obj);
      writer.flush();
      System.out.println(d);
  }
  </code></pre>
  
  <p>My JSP where I send JSON to server and try to receive JSON object response from server   </p>
  
  <pre><code>&lt;body&gt;
     &lt;h3&gt;Please enter a number to Square : &lt;/h3&gt;
     &lt;input style="width: 33px; margin-left: 2px; " type="text" id="number1" name="number1"&gt;
     &lt;input style="width: 33px; margin-left: 2px;" type="text" id="number2" name="number2"&gt;
     &lt;input type="button" onclick="callServlet();" value="Calc"&gt;
     &lt;input style="font-family: cursive; border:none" type="text"   id="result" /&gt;
     &lt;input style="font-family: cursive; border:none; width: 100%" type="text"  value=""  id="resultText" /&gt;
  
   &lt;/body&gt;
   &lt;script&gt;
          function callServlet() {
              var input1 = document.getElementById('number1').value;
              var input2 = document.getElementById('number2').value;
              var myData = {"mydata": {"number1": input1, "number2": input2}};
              $.ajax({
                  type: "GET",
                  url: "/AjaxServletCalculator",
                  data: {jsonData: JSON.stringify(myData)},
                  dataType: "json",
  
                  //if received a response from the server
                  success: function (data) {
                      //our country code was correct so we have some information to display
  
                          var json = JSON.parse(data);        
                          alert(json["resultText"]);        
                         /*document.getElementById('number1').value = data.*/        
                  }
              });
          }
   &lt;/script&gt;
  </code></pre>
  
  <p>pleas help find the problem on which the program does not work </p>