Search code examples
javascriptjavaeclipseservletsweb.xml

jQuery Ajax post method not successful (not able to find the path):


I am making an Ajax post method to send some data to my java servlet post method, but issue I am facing is that it is not able to find the path

Here is my HTML code:

<select name="outlet" id="myselect">
  <option>----------------------</option>
  <option>ALL</option>
</select>

And my jQuery (Ajax):

$("#myselect").on("change", function() {
  currentlyClickedOutlet = $(this).val();
  $.ajax({
    url: "./src/com/touchpoint/controller/DateWiseOlWiseSales.java",
    method: "POST",
    data: {
      Outlet: currentlyClickedOutlet,
    },
  });
});

And here is the file tree of my program:

this the program pattern

And on browser console it says error POST http://localhost:8080/TPWebReport/src/com/touchpoint/controller/DateWiseOlWiseSales.java 404

If I am giving URL:http://localhost:8080/TPWebReport/DateWiseOlWiseSales then its working fine but the main issue if I ll use this URL then it is for local host only on deployment on server it can cause problem and I don't know why this is happening.

I am using eclipse (2018-09) in eclipse mars 4.1.1 by putting only the java class name it was working fine like URL:DateWiseOlWiseSales it was able to find that class but not here in eclipse 2018-09.

here is my web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TouchPoint</display-name>
  <welcome-file-list>
    <welcome-file>Login.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.touchpoint.controller.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>com.touchpoint.controller.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/LogoutServlet</url-pattern>
  </servlet-mapping>
</web-app>

Solution

  • Put in the Ajax URL field only the servlet name: DateWiseOlWiseSales

    $.ajax({
           url : "DateWiseOlWiseSales",
           method : "POST",
    ....
    

    And add the following to your web.xml

    <servlet>
        <servlet-name>DateWiseOlWiseSales</servlet-name>
        <servlet-class>com.touchpoint.controller.DateWiseOlWiseSales</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>DateWiseOlWiseSales</servlet-name>
        <url-pattern>/DateWiseOlWiseSales</url-pattern>
      </servlet-mapping>