我有一个servlet
和jsp
页面。 jsp
页面包含最终用户将填写的表单。Tomcat 8:HTTP状态405 - 此方法不支持HTTP方法GET
<html>
<head>
<title>Please log in to your profile</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login_servlet" method="post">
Email: <input type="text" size="5" name="email"/>
Password: <input type="text" size="5" name="password"/>
<input type="submit" value="Sign In" />
</form>
</body>
</html>
那么我就要在servlet使用doPost()
方法,因为形式所具有的POST方法。我得到servlet中的参数,所以我可以打印到控制台。
@WebServlet("/login_servlet")
public class LoginServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
String email = req.getParameter("email");
String password = req.getParameter("password");
System.out.println("Email: " + email);
System.out.println("Password: " + password);
}
}
当我尝试访问的URL是http://localhost:8080/StudentPortal/login_servlet
我得到这个错误; HTTP Status 405 - HTTP method GET is not supported by this URL
,其描述为“请求的资源不允许指定的HTTP方法”。
我已经接近被阻止询问任何问题。因此,在此之前请标记为重复,我想让你知道我已经看过类似的问题,并且遵循了无效的建议。
我不得不学习servlets,因为我很快就被派上了Spring项目。
您能否链接到您遵循建议的类似问题,以便我们可以将这些解决方案排除在外。 –
当你直接在地址栏中输入servlet的路径时,你没有'doGet()'方法,像Tomcat这样的web容器会尝试调用'doGet()'方法。 – Mihir
@Mhhir'doGet()'方法应该有什么?现在我厌倦了将它添加到里面,我称它为'doPost()'; 'doPost(req,resp)'。 – TheRealRave