title: Sending json from servlet
body: <p>I write this servlet : </p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
      response.setContentType("application/json");
  
      BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
      String json = "";
      if(br != null){
          json = br.readLine();
      }
  
      PrintWriter out = response.getWriter();
  
      out.write(json);
      out.flush();
  }
  </code></pre>
  
  <p>Sending data via ajax request and the servlet me return them.
  The problem is that when I try to make me return a JSONObject created inside servlet gives me error 500.</p>
  
  <p>The code that gives me this error:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
  
  
  
      response.setContentType("application/json");
  
      BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
  
      JSONObject json = new JSONObject();
  
      json.put("name", "federico");
  
      PrintWriter out = response.getWriter();
  
      out.print(json);
      out.flush();
  }
  </code></pre>
  
  <p>My requestAjax is : </p>
  
  <pre><code>$.ajax({
                  url: "ValidServlet",
                  type: 'POST',
                  data : JSON.stringify(submitFormData),
                  success: function (data) {
                      alert(data.name);
                  },
                  error: function (data) {
                      alert('error');
                  }
              });
  </code></pre>
  
  <p>My error is :</p>
  
  <pre><code>Failed to load resource: the server responded with a status of 500 (Internal Server Error) (17:33:34:492 | error, network)at http://localhost:8080/ProgettoProsa/ValidServlet&gt; 
  </code></pre>
  
  <p>Maybe I'm wrong to return so the json?</p>
  
  <p>Help me figure out where I'm fail.</p>
  
  <p>Thank you all.</p>