title: ajax call to java servlet results in 404
body: <p>I'm trying to get a web page to send JSON data to a java servlet via a jQuery ajax POST.
  I've already checked everything I could think of, but I still can't figure out why I keep getting a 404.
  Even more confusing is that other calls to the same context path work correctly.</p>
  
  <p>My web.xml</p>
  
  <pre><code>&lt;web-app&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.vibridi.klyr.servlet.Controller&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;CustomerServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.vibridi.klyr.servlet.CustomerServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/klyr&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/home&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;CustomerServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/klyr/customer/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>My ajax call:</p>
  
  <pre><code>  $.ajax({
  url: "customer/save",
  type: "POST",
  data: JSON.stringify(o),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(obj) {
    alert('Customer saved');
  },
  error: function(obj) {
    alert('Error!');
  }
  </code></pre>
  
  <p>});</p>
  
  <p>My servlet:</p>
  
  <pre><code>public class CustomerServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;
  private static Logger logger = Logger.getLogger("KLYR_LOGGER");
  private CustomerManager manager;
  
  public void init(ServletConfig sconfig) throws ServletException {
      super.init(sconfig);
      manager = new CustomerManager();
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //stuff
  }
  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      PrintWriter out = response.getWriter();
      response.setContentType("application/json;charset=utf-8");
  
      try {
          StringBuffer sb = new StringBuffer();
          String line = null;
          BufferedReader reader = request.getReader();
          while((line = reader.readLine()) != null) {
              sb.append(line);
          }
  
          manager.saveCustomer(sb.toString());
  
      } catch(Exception e) {
          logger.log(Level.SEVERE, "Data processing failure: " + e.getMessage());
          out.write(Convertor.createBaseJSON(JSONType.E).toString());
          out.close();
      } 
  
      out.write(Convertor.createBaseJSON(JSONType.S).toString());
      out.close();
  
  }}
  </code></pre>
  
  <p>}</p>
  
  <p>I can see from the Chrome's debugger tools that the call is properly directed to <a href="http://localhost:8080/klyr/customer/save" rel="nofollow">http://localhost:8080/klyr/customer/save</a> but it 404's, whereas <a href="http://localhost:8080/klyr" rel="nofollow">http://localhost:8080/klyr</a> does not.</p>
  
  <p>Thanks a lot!</p>
  
  <p>EDIT:
  I've tried to switch the servlet mappings over, i.e. /klyr (the working one) on CustomerServlet and /customer/save on Controller, but nothing happens, in fact when I call /klyr from the browser bar instead of seeing the response from CustomerServlet.doGet I still see the welcome page as if Controller.doGet fired. It looks like tomcat isn't reloading the web.xml file even if I restart it. Any ideas? </p>