简体   繁体   中英

Ajax Returns 404 When Making Request to Servlet

I currently have apache tomcat running on my Ubuntu AWS EC2 server. I have all my html/js exclusive webapps served there, and those work fine. I am currently trying to import my Java project, but I cant get my AJAX calls to reach my java (I'm getting a 404 error). What is weird to me is that my local version works perfectly fine with making these AJAX requests. I suspect I may have a firewall blocking me after doing this Tomcat SSL Guide . I'll post some code, maybe I have a logic mistake.

web.xml

I used to not have this commented out, but I tried annotations. This worked on local, but not on the server.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<!-- <servlet>
    <servlet-name>response</servlet-name>
    <servlet-class>Demo.ServiceReceiver</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>response</servlet-name>
    <url-pattern>/sendIt</url-pattern>
</servlet-mapping> -->
</web-app>

ServiceReceiver.java

@WebServlet("/sendIt")
public class ServiceReceiver extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String first = request.getParameter("stuff");
    String second = request.getParameter("other");

    Driver driver = new Driver();

    String ret = driver.runner();
    response.getWriter().write(ret);
}
}

index.js

$(document).ready(function() {
$("#testClick").click( function()
    {
        testerClick();
    }
);
});

function testerClick() {
var send;
send = {
    stuff:"ayyyye",
    other:"naayyyyyy"
};

$.ajax({
    url: 'sendIt',
    data: send,
    type:'GET',
    cache: false,
    contentType: "application/json; charset=utf-8",
    success:function(data){
        alert(data);
    },
    error:function(error){
        alert(error);
    }
});
}

Again, this all works on local, so I suspect I'm unknowingly blocking myself. The website I am trying to reach is https://tyleralangreen.com/Dice_Game_war/ . If you click the button, you will see that you are given a 404. The address looks right: https://tyleralangreen.com/Dice_Game_war/sendIt?stuff=ayyyye&other=naayyyyyy&_=1521266411409

I have found the issue. I went through all my tomcat logs looking for errors, and one tiny error said that it wasn't going to compile my Java class. Oh wow, thanks for the near silent error. Anyways the issue said:

org.apache.catalina.core.ApplicationContext.log Marking servlet [Demo.ServiceReceiver] as unavailable 19-Mar-2018 01:26:15.444 SEVERE [ajp-nio-127.0.0.1-8009-exec-8] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet [Demo.ServiceReceiver] java.lang.UnsupportedClassVersionError: Demo/ServiceReceiver has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0 (unable to load class [Demo.ServiceReceiver])

I looked and I am compiling with Java 9 on intellij, but I have Java 8 installed on my server. I changed my default JDK on IntelliJ to Java 8 and everything worked. I hope I help others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM