title: jQuery - AJAX call of Servlet
body: <p>I'm building a web app for store scientific publications. I've used MySQL, JSP, jQuery and JAVA.</p>
  
  <p>I have a form inside a JSP page, like this:</p>
  
  <pre><code>&lt;form method="get"&gt;
      &lt;input type="text" id="code"&gt;
      &lt;button onclick="request()"&gt;Search&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The function <strong>request()</strong> is:</p>
  
  <pre><code>function request() {
      var code = $('#code').val();
      var data='code='+code;
      $.ajax({
            url: "Servlet",
            type: "GET",
            data: data,
            success: function(){
                alert("success");
            },
            error:function(){
                alert("failure");
            }   
          }); 
  };
  </code></pre>
  
  <p>The Servlet do some stuff and then save the resultset of query in the request object and forward the result on the index like this</p>
  
  <pre><code>request.setAttribute("result", resultSet);
  RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/index.jsp");
  dispatcher.forward(request, response);
  </code></pre>
  
  <p>The <strong>index.jsp</strong> contains:</p>
  
  <pre><code> &lt;%  if(request.getAttribute("result")!=null){ %&gt;
          &lt;jsp:include page="table.jsp" /&gt;
    &lt;%}%&gt;
  </code></pre>
  
  <p>check if the result set is already filled, then include table.jsp, the page scan the resultset and put the values in a table.</p>
  
  <p>This code doesn't work, someone can help me?
  1- The query runs correctly
  2- jQuery is called correctly</p>
  
  <p>but the </p>
  
  <pre><code>if(request.getAttribute("result"))
  </code></pre>
  
  <p>is always null.</p>