gpt4 book ai didi

tomcat - POST 请求时 doGet 中的 Servlet 错误

转载 作者:行者123 更新时间:2023-11-28 22:17:02 24 4
gpt4 key购买 nike

我正在使用 Tomcat 服务器构建一个带有聊天功能的网站。对于那个聊天,我有一个调用 JS 函数的表单(带有文本输入),它执行 jQuery POST 请求...

<!DOCTYPE html>

<html>
<body>
<form method="post" action="javascript:submitNewMessage()" accept-charset="UTF-8" id="msgForm">
<input type="text" name="msg" id="msgField"/>
<input type="submit" name="submit" value="Send"/>
</form>

<script src="jquery.js"></script>
<script>
"use strict";

function submitNewMessage() {
var msg = document.getElementById("msgField").value;
$.post("chatController", {message:msg, author:"${user.username}", gameId:"${game}", chat:"${chat.type}"});
}
</script>
</body>
</html>

...到这个 Controller ,chatController.java:

@WebServlet(urlPatterns = {"/chatController"})
public class ChatController extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("user", new User(request.getParameter("user")));
request.setAttribute("game", Integer.parseInt(request.getParameter("gameId")));
request.setAttribute("chat", new ChatRoom(ChatRoomType.fromString(request.getParameter("chat"))));
request.setAttribute("lastMessage", 0);

request.getRequestDispatcher("/WEB-INF/chat.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
ChatDAO dao = new ChatDAO(this.ds);
if (dao.submitMessage(...) // not implemented yet, returns false) {
throw new ServletException();
}
} catch (Exception e) {
response.sendError(400);
}
}
}

当用户加入聊天时使用chatController的GET,然后只使用POST提交消息。当我加入聊天页面时,没有错误。但是当我提交消息时,(Firefox 的)调试器告诉我 XML 解析错误:第 1 行未找到元素。 chatController 返回的 HTML 在 doGet 方法中包含一个 Java 错误(NumberFormatException: null,在 doGet 的第二行,我在其中执行 Integer.parseInt)。

为什么这里调用doGet方法?我在 chatController 上执行 POST 请求...当我在 doGet 中打印某些内容时,它没有出现在日志中:那么为什么该行会抛出错误?该代码有什么问题?

感谢您的帮助。

最佳答案

您的 jsp 和 servlet 在我的本地运行良好。可能发生了 JavaScript 错误。您是否在适当的位置部署了 jquery.js?

关于tomcat - POST 请求时 doGet 中的 Servlet 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101833/

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