2016-06-28 477 views
0

我有一个servletjsp页面。 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"/> 
     &nbsp;&nbsp; 
     Password: <input type="text" size="5" name="password"/> 
     &nbsp;&nbsp; 
     <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项目。

+0

您能否链接到您遵循建议的类似问题,以便我们可以将这些解决方案排除在外。 –

+0

当你直接在地址栏中输入servlet的路径时,你没有'doGet()'方法,像Tomcat这样的web容器会尝试调用'doGet()'方法。 – Mihir

+0

@Mhhir'doGet()'方法应该有什么?现在我厌倦了将它添加到里面,我称它为'doPost()'; 'doPost(req,resp)'。 – TheRealRave

回答

0

当您访问您的网址时,您正在使用get方法。 而在你的servlet中你只声明了doPost方法。 尝试使用邮递员扩展程序来使用其他http方法。

*尝试直接访问到你的JSP页面:localhost:8080/StudentPortal/pathToYourJspPage

*您也可以在你的servlet添加doGet方法和呼叫重定向从那里到jsp页面:response.sendRedirect是(位置)或的request.getRequestDispatcher(位置).forward(request,response)

+0

但为什么我需要下载扩展名?当然,它一定是我的代码有问题。 – TheRealRave

+0

那么,你应该直接进入你的jsp页面,而不是servlet。通过'http:// localhost:8080/StudentPortal/login_servlet'进入servlet。尝试使用此网址:http:// localhost:8080/StudentPortal/pathToYourJspPage – mrserfr

+0

虽然这不是坏习惯吗?假设你有'... home.jsp',当你尝试访问像home/profile/activity这样的东西时,你会不会遇到问题? – TheRealRave

相关问题