Tomcatサーバーを使用してチャットしてウェブサイトを構築しています。そのチャットのために、私は、このコントローラに... chatController.javaをjQueryのPOSTリクエストを行い、JS関数を呼び出します(テキスト入力を持つ)フォーム...POST要求時にdoGetでサーブレットエラーが発生する
<!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>
を持っている:
@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 parsing error : no element found at line 1
と伝えます。 chatControllerが返すHTMLには、doGetメソッドのJavaエラーが含まれています(NumberFormatException: null
、doGetの2行目、Integer.parseInt
)。
ここでdoGetメソッドが呼び出されるのはなぜですか?私はchatControllerでPOSTリクエストを行います...そして、doGetで何かを印刷すると、ログには表示されません:なぜ、その行はエラーを投げていますか?そのコードの何が間違っていますか?
ありがとうございました。
Firefoxのデバッガかもしれませんか?あなたのサーブレットで誰がリクエストを出しているのか知る方法はありますか? – PiggyGenius
私のログによれば、GET要求は1つだけです(これは期待される動作です)。だから私は、Firefoxのデバッガが信頼できるかどうかわかりません... –