簡體   English   中英

Java JSP Jquery AJAX Servlet

[英]Java JSP Jquery AJAX Servlet

UPDATE

我不同意所建議問題的答案將直接應用於此問題,因為我使用的是非標准目錄結構。

/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

我在使用Jquery AJAX的應用程序中嘗試使用JSP Servlet時有些困惑。

我很可能會錯過一個步驟(或完全錯誤地執行)。

我有我的看法

/public/view/nothing/nothing01.jsp

<% include file="../partials/header.jsp" %>
<button id="buttTest">Test</button>
<% include file="../partials/footer.jsp" %>

鏈接到旨在用於對我的Java Servlet進行Jquery AJAX調用的Javascript文件

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

}

然后我有我的源代碼並編譯了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");
     }
}

編譯正常,沒有任何錯誤,但是我不確定如何使它可用於Jquery AJAX請求,因為該請求以404找不到錯誤為響應。 如果我嘗試指定AJAX網址以指向Java和類文件,則它只會提取內容而不是執行內容。

UPDATE

我將包括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>

您的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));
                   }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM