POST请求时doGet中的Servlet错误

时间:2017-03-29 18:52:22

标签: tomcat servlets post web get

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

@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.java:

XML parsing error : no element found at line 1

当用户加入聊天时使用chatController的GET,然后只使用POST来提交消息。当我加入聊天页面时,没有错误。但是当我提交消息时,(Firefox的)调试器会告诉我NumberFormatException: null。 chatController返回的HTML在doGet方法(Integer.parseInt中包含一个Java错误,在doGet的第二行,我在那里public static class Matcher<T> { Predicate<T> predicate; Class<T> clazz; public Matcher(Predicate<T> t, Class<T> clazz) { this.predicate = t; this.clazz = clazz; } @SuppressWarnings("unchecked") public boolean equals(Object o) { if (o != null && this.clazz.isInstance(o)) { System.out.println("Checking item " + o + " against predicate " + this.predicate); return this.predicate.test((T) o); } return false; } } public static void main(String args[]) { ArrayBlockingQueue<Integer> oddsOnly = new ArrayBlockingQueue<>(10); oddsOnly.addAll(Arrays.asList(1, 3, 5, 7, 9, 11, 13)); ArrayBlockingQueue<Integer> oddsAndEves = new ArrayBlockingQueue<>(10); oddsAndEves.addAll(Arrays.asList(1, 2, 3, 4, 5)); Predicate<Integer> evenPredicate = (i) -> i % 2 == 0; System.out.println(oddsOnly.contains(new Matcher<>(evenPredicate, Integer.class))); // prints false System.out.println(oddsAndEves.contains(new Matcher<>(evenPredicate, Integer.class))); // prints true } )。

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你的jsp和servlet在我的本地工作正常。可能是发生了JavaScript错误。您是否在适当的位置部署了jquery.js?