title: How to get parameters in servlet from ajax request?
body: <p>This is the client code: </p>
  
  <pre><code>function save(calc){
      var request = new XMLHttpRequest();
  
      request.open("POST","/Calculator/Controller?action=saveCalc",true);
      //request.send("calc=calc")   ---this does not work also
      request.send(calc);
  }
  </code></pre>
  
  <p>this is the servlet:</p>
  
  <pre><code>if (action.equals("saveCalc")) {
          String parameter = request.getParameter("calc");
          System.out.println(parameter);
  }
  </code></pre>
  
  <p>The parameter here is null...</p>
  
  <p>I also tried like that: </p>
  
  <pre><code>function save(calc){
          var request = new XMLHttpRequest();
          request.open("POST","/Calculator/Controller?action=saveCalc",true);
          var data = new FormData();
          data.append('calc', 'calc');
          request.send(data);
      }
  </code></pre>
  
  <p>also does not work.</p>
  
  <p>How to get this .... parameter. :D</p>
  
  <p>I succseed. Here is the solution: - content-type ...</p>
  
  <pre><code>function save(calc){
      var request = new XMLHttpRequest();
      request.open("POST","/Calculator/Controller?action=saveCalc",true);
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      request.send("calc=" + calc);
  }
  
  
  protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          String action = request.getParameter("action");
  
          if (action.equals("saveCalc")) {
              String calc = request.getParameter("calc");
              System.out.println(calc);
          }
  }
  </code></pre>