title: Send form data from jsp to servlet using ajax
body: <p>I spent 10 years writing ASP/Ajax and MUST switch to JAVA. 
  I am starting very simple script so as to understand the concepts clearly before diving into more complex stuff. I am using Tomcat 7, Servlet 3</p>
  
  <p>The problem is. The servlet is called successfully but the form data is not transferred</p>
  
  <p>This is the edited servlet (Main.java)</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class Main
   */
  @WebServlet("/Main")
  public class Main extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public Main() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException {
         // TODO Auto-generated method stub
      //int ids;
      //String ids;
      //response.setContentType("text/html;charset=UTF-8");
  
      //PrintWriter out = response.getWriter();
      String val = request.getParameter("id");
      String name = request.getParameter("name");
      //response.getWriter().write(val);
      //response.getWriter().write("::");
      //response.getWriter().write(name);
  
      response.setContentType("text/plain");
      response.setCharacterEncoding("UTF-8");
      response.getWriter().write(val +" "+ name);
  
      //System.out.print(val);
      //if(val != null){
       //   //ids = Integer.parseInt(val);
        //  //out.print(ids);
         // out.print(val);
      //}
  }
  }
  </code></pre>
  
  <p>This is the edited JSP (Main.jsp):</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;title&gt;SO question 4112686&lt;/title&gt;
      &lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
  
      &lt;script&gt;
  $.ajaxSetup({ cache: false });
  $(document).ready(function() {   
  $('#submit').click(function(event) {
      var form = (event.target.form),
          url = "/Main"
              + "?lName=" + escape(form.elements.lName.text)
              + "&amp;fName=" + escape(form.elements.fName.text);
  
      $.get(url, function(getData) { 
          $('#somediv').text(getData);         
      });
  });
  });
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="Main" action="Main" method="post"&gt;
  &lt;h4&gt;AJAX Demo using Jquery in JSP and Servlet&lt;/h4&gt;
  Enter your Name:
  &lt;br/&gt;&lt;input type="text" id="fName" name="fName"/&gt;
  &lt;br/&gt;&lt;input type="text" id="lName" name="lName"/&gt;
  &lt;br/&gt;&lt;input type="submit" id="submit" value="Ajax Submit"/&gt;
  &lt;br/&gt;
  &lt;/form&gt;
  
  &lt;div id="somediv"&gt;....&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>