“HTTP method POST is not supported by this URL ”
之前练习的时候,写了个LoginServlet继承的HttpServlet的小例子,运行的时候一直报HTTP method POST is not supported by this URL,代码与报错如下,控制台没有报错:
提示不支持post方法,但是代码里已经设置了post方法,于是有点懵逼,之后查了一下HttpServlet源码里的doPost方法
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String protocol = req.getProtocol();
String msg = lStrings.getString(“http.method_post_not_supported”);
if (protocol.endsWith(“1.1”)) {
resp.sendError(405, msg);
} else {
resp.sendError(400, msg);
}
}
也就是说不管你的http是不是1.1的,都是用resp.sendError方法返回一个“http.method_post_not_supported”的错误信息给前台界面。也就是说问题出在super.doPost这里,去掉就能正常运行了。一开始没看过源码,直接写的时候,eclipse自动带出来就懒得删,折腾了我半天,java源码也是,还有这种幺蛾子,记下来,免得下次有爬这个坑(或许这么写有什么玄机我没参透,功力有限)。
还没有评论,来说两句吧...