问题:HTTP Status 405 - HTTP method POST is not supported by this URL
原因:form表单中method为post,但servlet中没有用doPost方法,而是用的doGet方法。
示例:
<form action="prin" method="post">
<p>用户名:<input type="text" name="username" /> <br> </p>
<p>密码: <input type="password" name="password" /> <br> </p>
<button>提交</button>
</form>
错误方法:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
System.out.println(method);
}
正确方法:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
System.out.println(method);
}
注意:form表单中get方法对应servlet中doGet方法,post方法对应servlet中doPost方法。