gpt4 book ai didi

javascript - click 函数不引用 servlet (IDEA, java)

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:19 25 4
gpt4 key购买 nike

命令 $.get('MyController1', function (responseText) 不会发送到我的 servlet。我使用 ajax,单击按钮后它应该发送到我的 servlet('MyController1' 或 'MyController2'...),servlet 发送到 .jsp-page 并且应该出现一些文本。这是我的起始页(index.jsp):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<script>
$(document).ready(function () {
$('#1').click(function () {
$.get('MyController1', function (responseText) {
$('#main').html(" ");
$('#main').append(responseText);
});
});
});
</script>

<script>
$(document).ready(function () {
$('#2').click(function () {
$.get('MyController2', function (responseText) {
$('#main').html(" ");
$('#main').append(responseText);
});
});
});
</script>
</head>
<body>
<input type="button" value="ButtonTest1" id="1">
<input type="button" value="ButtonTest2" id="2">
<div id="main"></div>
</body>
</html>

这是我的 web.xml 的一部分:

<servlet>
<servlet-name>MyController1</servlet-name>
<servlet-class>MyController1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyController1</servlet-name>
<url-pattern>/myController1</url-pattern>
</servlet-mapping>

这是我的 servlet:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyController1 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/button1.jsp").forward(req, resp);

}
}

我做错了什么?

最佳答案

您需要将 AJAX 调用修复为

$.get('myController1', function (responseText) {
^^ lowercase m

匹配<url-pattern>配置为MyController1您的 web.xml 中的 servlet .

关于javascript - click 函数不引用 servlet (IDEA, java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31234729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com