title: Null or empty JSON object from AJAX request
body: <p>Ended up deleting another question I had posted earlier related to this, as it had become obsolete and there was another thing to address. The answer below is very clear, more so than other answers, and I believe this will be helpful for anyone else having similar confusion.</p>
  
  <p>Essentially I want to use a String in Java that comes from client side JS. My JS alerts below work fine, however my JSON object is null or empty in my Servlet. I really just need one string passed to my servlet so I can run SQL queries, but am having a lot of trouble with it.</p>
  
  <p>Thank you!</p>
  
  <p>html file</p>
  
  <pre><code>       &lt;div class="jumbotron" id="jumboframe"&gt;
              &lt;h1&gt;Enter your URL below&lt;/h1&gt;
              &lt;input class="txtUrl" type="text" id="txtUrl" value="...." onfocus="if(this.value == '....') { this.value = ''; }"/&gt;
              &lt;p&gt;&lt;a class="btn btn-lg submitButton" href="testpage.html" onclick="getURL()" role="button"&gt;Start searching!&lt;/a&gt;&lt;/p&gt;
          &lt;/div&gt;
  
          &lt;script&gt;
              function getURL() {
                  var urlString = document.getElementById("txtUrl").value;
                  var params = {url: urlString};
  
  
              $.ajax({
                      type: "POST",
                      url: "testpage.html",
                      contentType: "application/json",
                      dataType: "text", // "JSON" here doesn't call alerts below
                      data: {
                          'url': urlString
                      },
  
                      success: function(data) {
                          alert("did it!");
                          alert(JSON.stringify(params));
                          alert(JSON.stringify(data));
                      }
                  });
              }
          &lt;/script&gt;
  </code></pre>
  
  <p>Java servlet</p>
  
  <pre><code>public class JavaServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  
      doPost(request, response);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  
      PrintWriter out = response.getWriter();
  
      StringBuilder builder = new StringBuilder();
      BufferedReader reader = request.getReader();
      String line;
      while ((line = reader.readLine()) != null) {
          builder.append(line);
      }
  
      out.println(builder.toString());
      String url = request.getParameter("url");
      Map&lt;String, String[]&gt; map = request.getParameterMap();  // empty {}
      out.println(map.toString());  // null
      out.println(url);  // null
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>  &lt;servlet&gt;
        &lt;servlet-name&gt;JavaServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;path.to.my.JavaServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;JavaServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/testpage.html&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Thanks again</p>