title: Java ajax passing values from jsp to servlet
body: <p>I am trying to pass basic values such as id from jsp to the servlet through ajax. I tried everything but only null is being passed. Even <code>console.log(val)</code> does not print anything to browser console. </p>
  
  <p><em>My understanding is:</em> Web page has form values which onsubmit calls js file. js has ajax which calls the servlet and passes the data of the form. The servlet grabs data from ajax by <code>request.getParameter(val)</code> </p>
  
  <p>Here is my code:</p>
  
  <p><strong>Main.jsp</strong></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&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=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"&gt;
  &lt;script src="js/main.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form method="post" action="Main" id="firstform"&gt;
      &lt;h1&gt;Enter name:&lt;/h1&gt;
      &lt;input type="text" name="id" id="id" /&gt;
      &lt;input type="submit" name="submit"/&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>main.js</strong></p>
  
  <pre><code>var form = $('#firstform');
  console.log("gi");
  form.submit(function()
  {
      $.ajax({
          url: 'Main',
          data: form.serialize(),
          type: 'post',
          success: function(data){ 
              console.log(data);
          }
              });
  
      //return false;
  
  });
  </code></pre>
  
  <p><strong>Main.java </strong></p>
  
  <pre><code>package servlets;
  
  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 doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          int ids;
          response.setContentType("text/html;charset=UTF-8");
  
          PrintWriter out = response.getWriter();
          String val = request.getParameter("id");
          System.out.print(val);
          if(val != null){
              ids = Integer.parseInt(val);
              out.print(ids); //
          }
  
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
  }
  </code></pre>
  
  <p>**Problems:<br>
  1)values passed from jsp to servlet<br>
  2)console.log doesnt print anything on browser console</p>
  
  <p>1) works but 2) still doesnt.**</p>