0

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.

My web.xml

<web-app>
<servlet>
    <servlet-name>Controller</servlet-name>
    <servlet-class>com.vibridi.klyr.servlet.Controller</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>CustomerServlet</servlet-name>
    <servlet-class>com.vibridi.klyr.servlet.CustomerServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/klyr</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/home</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>CustomerServlet</servlet-name>
    <url-pattern>/klyr/customer/*</url-pattern>
</servlet-mapping>

My ajax call:

  $.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!');
}

});

My servlet:

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();

}}

}

I can see from the Chrome's debugger tools that the call is properly directed to http://localhost:8080/klyr/customer/save but it 404's, whereas http://localhost:8080/klyr does not.

Thanks a lot!

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?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
  • I don't see any servlet config with the url as `customer/save`. Try to hit the url from the browser directly using the address bar. –  Oct 31 '15 at 08:30

2 Answers2

0

This is obvious because your CustomerServlet does not bind to $.ajax({url: "customer/save", ... so it won't work, your should change the below code :

<servlet-mapping>
     <servlet-name>CustomerServlet</servlet-name>
     <url-pattern>/klyr/customer/*</url-pattern>
</servlet-mapping>

to something like:

<servlet-mapping>
     <servlet-name>CustomerServlet</servlet-name>
     <url-pattern>/customer/save</url-pattern>
</servlet-mapping>

in order to solve the problem ~

Mehdi
  • 3,795
  • 3
  • 36
  • 65
0

I've eventually found the culprit, gonna post it here as a reference for other people. Both servlets mapped in my web.xml are loaded on startup. The first servlet attempted to read a config file from an incorrect path inside its init() method, but couldn't find it and threw an exception. The Catalina startup routine exited before it could load the second servlet, hence the 404 error.

blackgreen
  • 34,072
  • 23
  • 111
  • 129