Reputation: 4094
UPDATE
I disagree that the answer for the suggested question will directly apply to this question because I am using a non standard directory structure.
/opt/tomcat/webapps/nothing/
/public/
/views/
/home/
home.jsp
/nothing/
nothing01.jsp
/partials/
header.jsp
footer.jsp
/logic/
/nothing/
nothing01.js
nothing01test.java
nothing01test.class
/resources/
/WEB-INF/
web.xml
I'm having a bit of confusion trying to use a JSP Servlet in my application using Jquery AJAX.
I'm most likely missing a step (or doing it completely wrong).
I have my view
/public/view/nothing/nothing01.jsp
<% include file="../partials/header.jsp" %>
<button id="buttTest">Test</button>
<% include file="../partials/footer.jsp" %>
Which is linked to a Javascript file intended to use in making a Jquery AJAX call to my Java Servlet
/public/logic/nothing/nothing01.js
$(document).ready(function() {
$("#buttTest").on("click", function() {
ajaxTest();
});
});
function ajaxTest() {
$.ajax ({
url : "nothing01test",
type : "GET",
cache : false,
dataType : "json",
success : function(results) {
console.log("success");
console.log(JSON.stringify(results));
},
error : function(results) {
console.log("error");
console.log(JSON.stringify(results));
}
});
}
Then I have my source and compiled Java Servlet
/public/logic/nothing/nothing01test.java
/public/logic/nothing/nothing01test.class
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class nothing01test extends HttpServlet {
protected void doGet (
HttpServletRequest request,
HttpServletResponse response
) throws
ServletException,
IOException
{
PrintWriter out = response.getWriter();
out.println("AJAX RESPONSE");
}
}
The compile works fine without any errors but I'm not sure how to make it available to the Jquery AJAX request because the request is responding with a 404 not found error. If I try specifying the AJAX url to point to the java and class file it just pulls in the contents instead of executing.
UPDATE
I'm going to include the web.xml file just in case
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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_4_0.xsd"
version="4.0"
metadata-complete="true">
<display-name>Java Web App</display-name>
<description>
Welcome to Java Web App
</description>
<welcome-file-list>
<welcome-file>public/views/home/home.jsp</welcome-file>
</welcome-file-list>
</web-app>
Upvotes: 0
Views: 1057
Reputation: 10633
Your AJAX call is incorrect, try replacing it with the code below
$(document).ready(function() {
$("#buttTest").on("click", function() {
ajaxTest();
});
});
function ajaxTest() {
$.ajax({
url : "nothing01test",
type : "GET",
cache : false,
dataType : "json",
success : function(results) {
console.log("success");
console.log(JSON.stringify(results));
},
error : function(results) {
console.log("error");
console.log(JSON.stringify(results));
}
});
}
Upvotes: 1