title: Return JSON from servlet
body: <p>It is a very basic request-response test. Browser sends "hello from browser" to servlet using jQuery $.ajax API, and servlet receives this message, then create a JSON object using org.json.simple library and sends back to browser a JSON response with message "hello from server".</p>
  
  <p>I am running this on localhost and just assume my IP address is 123.123.12.123, the platform is Ubuntu, server is Tomcat 6.0, running in the Eclipse IDE.</p>
  
  <p>Test 1. I start the server from Eclipse, open Firefox, enter <a href="http://localhost:8080/myproject/test.jsp" rel="nofollow noreferrer">http://localhost:8080/myproject/test.jsp</a>, I can see servlet receives message and browser receives response, test passed.</p>
  
  <p>Test 2. server is still running at the Eclipse at Ubuntu, I start Windows 7 guest machine from VirtualBox and the Firefox browser in the Windows 7, enter <a href="http://123.123.12.123:8080/myproject/test.jsp" rel="nofollow noreferrer">http://123.123.12.123:8080/myproject/test.jsp</a>, works as I expected, test passed.</p>
  
  <p>Test 3. server is still running at Eclipse at Ubuntu, open Internet Explorer 9 browser, give it address <a href="http://123.123.12.123:8080/myproject/test.jsp" rel="nofollow noreferrer">http://123.123.12.123:8080/myproject/test.jsp</a>, <b>nothing happens.</b>
  The debug gives me </p>
  
  <p>Response    HTTP/1.1 200 OK</p>
  
  <p>Response body {"message":"hello from server"}</p>
  
  <p>The test.jsp is</p>
  
  <pre>
  &lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="release/js/libs/json2.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
      var request = ({"message":'Hello from browser'});
      var jsonobj=JSON.stringify(request);
      $.ajax({
          data: {para:jsonobj},
          dataType: 'json',
          url: './TestServlet',
          type: 'POST',
          success: function(jsonObj){
              alert(jsonObj.message);     
          },
          error: function() {
              alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err);
          }
      });
  });
  &lt;/script&gt;
  &lt;body&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </pre>
  
  <p>The servlet code is</p>
  
  <pre>
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.json.simple.JSONObject;
  import org.json.simple.JSONValue;
  
  /**
   * Servlet implementation class TestServlet
   */
  public class TestServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      public TestServlet() {
          super();
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          request.setCharacterEncoding("utf8");
          response.setCharacterEncoding("utf8");
          response.setContentType("application/json"); 
          PrintWriter out = response.getWriter(); 
          JSONObject jsonObj = (JSONObject) JSONValue.parse(request.getParameter("para"));
          System.out.println(jsonObj.get("message"));         
          JSONObject obj = new JSONObject();
          obj.put("message", "hello from server");
          out.print(obj);
  
      }
  
  }
  </pre>
  
  <p>Update: </p>
  
  <p>After a closer look by change </p>
  
  <pre>
   error: function() {
              alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err);
  }
  </pre>
  
  <p>to</p>
  
  <pre>
  error: function(xhr,err) {
              alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err);
          }
  </pre>
  
  <p>I got alert readyState:0 and status:0.
  But I can see {"message":"hello from server"} at Response body and 
  the response header is</p>
  
  <pre>
  Key Value
  Response    HTTP/1.1 200 OK
  </pre>