javajspjqueryajaxservlet

javajspjqueryajaxservlet,java,jquery,ajax,jsp,servlets,Java,Jquery,Ajax,Jsp,Servlets,更新 我不同意建议问题的答案将直接适用于此问题,因为我使用的是非标准目录结构 /opt/tomcat/webapps/nothing/ /public/ /views/ /home/ home.jsp /nothing/ nothing01.jsp /partials/ header.jsp

更新

我不同意建议问题的答案将直接适用于此问题,因为我使用的是非标准目录结构

/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
在使用jqueryajax的应用程序中使用jspservlet时,我有点困惑

我很可能错过了一个步骤(或者完全做错了)

我有我的看法

/public/view/nothing/nothing01.jsp

<% include file="../partials/header.jsp" %>
<button id="buttTest">Test</button>
<% include file="../partials/footer.jsp" %>
然后我有了我的源代码和编译的Javaservlet

/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));
                   }
    });

}
/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");
     }
}
编译工作正常,没有任何错误,但我不确定如何使其可用于jQueryAjax请求,因为请求响应404NotFound错误。如果我尝试指定ajaxurl来指向java和类文件,它只会拉入内容而不是执行

更新

我将包含web.xml文件以防万一

/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>
/WEB-INF/WEB.xml
Java Web应用程序
欢迎使用Java Web App
public/views/home/home.jsp

您的AJAX调用不正确,请尝试用下面的代码替换它

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

你的web.xml是正确的,我想(?)我没有对web.xml做任何事,我应该做吗?特伦特,你的
nothing01test
servlet没有映射到任何URL是真的吗?参考问题中的第二个部分提到了这一点。搜索以“将此servlet映射到URL模式”开头的段落,这是复制粘贴错误,谢谢注意