title: Ajax request does not reach the servlet
body: <p>I am trying to ajax call using jquery and jsp but I can't make it work.</p>
  
  <pre><code>&lt;%@page import="in.isuru.twitter.Twits" %&gt;
  &lt;%@page import="java.util.ArrayList" %&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Twitter Sinhala&lt;/title&gt;
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="script.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form id="form" action="twitterv2" method="post"&gt;
              Enter Something
              &lt;input id="term" type="text" name="term" /&gt;
              &lt;input id="submit" type="submit" value="Show results" name="submit"/&gt;
          &lt;/form&gt;
          &lt;p id="result"&gt;&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is the servlet.</p>
  
  <pre><code>package in.isuru.twitter;
  
  import java.io.IOException;
  import java.util.ArrayList;
  
  import javax.servlet.http.*;
  
  import twitter4j.Query;
  import twitter4j.QueryResult;
  import twitter4j.Tweet;
  import twitter4j.Twitter;
  import twitter4j.TwitterException;
  import twitter4j.TwitterFactory;
  
  @SuppressWarnings("serial")
  public class TwitterV2Servlet extends HttpServlet {
  
      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
          resp.setContentType("text/html");
          //Twits tweets = new Twits();
          Twitter twitter = new TwitterFactory().getInstance();
          //twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
          Query query = new Query("man");
          QueryResult result = null;
          try {
              result = twitter.search(query);
          } catch (TwitterException e) {
              e.printStackTrace();
          }
          for (Tweet tweet : result.getTweets()) {
              resp.getWriter().println(tweet.getFromUser() + ":" + tweet.getText() + "  &lt;br/&gt;");
          }
      }
  
  }
  </code></pre>
  
  <p>This is script.js</p>
  
  <pre><code>$(document).ready(function() {
      $('#form').submit(function() {
          var number = $('#term').val();
  
          $.ajax({
              type:       "post",
              url:        "twitterv2",
              data:       "term=" + term,
              success:    function(msg) {
                  $('#result').hide();
                  $("#result").html("&lt;h3&gt;" + msg + "&lt;/h3&gt;").fadeIn("slow");
              }
          });
  
          return false;
      });
  });
  </code></pre>
  
  <p>This is web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;TwitterV2&lt;/servlet-name&gt;
          &lt;servlet-class&gt;in.isuru.twitter.TwitterV2Servlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;TwitterV2&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/twitterv2&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I cannot make it work. When I go to the servlet page and give argument like
  <a href="http://localhost:8888/twitterv2?term=people" rel="nofollow">http://localhost:8888/twitterv2?term=people</a> it shows twitter posts. So there is no errors in the servlet, I think the problem is in script.js. But I can't find an error.</p>