我有一个 servlet
和一个 jsp
页面。 jsp
页面包含最终用户将填写的表单。
<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);
}
}
当我尝试访问 http://localhost:8080/StudentPortal/login_servlet
的 URL 时,我收到此错误; HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET
,描述为“请求的资源不允许指定的 HTTP 方法。”
我差点被禁止再提问了。因此,在将其标记为重复之前,我想让您知道我已经看过类似的问题并遵循了无济于事的建议。
我必须学习 servlet,因为我马上要参加 Spring 项目工作。
最佳答案
当您访问您的 url 时,您正在使用 get
方法。
在您的 servlet 中,您只声明了 doPost
方法。
尝试使用 postman extension for chrome 以使用其他 http 方法。
*尝试直接访问你的jsp页面:localhost:8080/StudentPortal/pathToYourJspPage
*您还可以在您的 servlet 中添加 doGet
方法并从那里调用重定向到 jsp 页面:response.sendRedirect(location) 或 request.getRequestDispatcher(location).forward(request,response)
关于java - Tomcat 8 : HTTP Status 405 - HTTP method GET is not supported by this URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38071998/