获取错误HTTP状态405 – 此方法不支持HTTP方法GET但不使用`get`?

我是初学者并使用数据库制作一个小型注册程序但是我正在尝试运行这个但是它给了我一些错误请帮忙:

HTTP Status 405 - HTTP method GET is not supported by this URL type Status report message HTTP method GET is not supported by this URL description The specified HTTP method is not allowed for the requested resource. Apache Tomcat/8.0.5 

这是我的register.html代码:

       
Name: Email: Password: Country: India Pakistan Other

这是我的Register.java代码:

 import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Register extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter out=response.getWriter(); String n=request.getParameter("name"); String p=request.getParameter("password"); String e=request.getParameter("email"); String c=request.getParameter("userCountry"); try{ Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:8888", "root", "1234" ); PreparedStatement ps=con.prepareStatement("insert into REGISTERUSER values(?, ?, ?, ?)"); ps.setString(1,n); ps.setString(2,p); ps.setString(3,e); ps.setString(4,c); int i=ps.executeUpdate(); if(i>0){ out.print("Registered successfully.."); } }catch(Exception d){d.printStackTrace();} out.close(); } } 

这是我的Web.xml

    Register Register   Register /register.html   register.html   

帮助将不胜感激!!

问题是您将servlet映射到/register.html并且它期望POST方法,因为您只实现了doPost()方法。 所以当你打开register.html页面时,它不会打开带有表单的html页面,而是打开处理表单数据的servlet。

或者,当您将POST表单提交到不存在的URL时,Web容器将显示405错误(不允许方法)而不是404(未找到)。

修理:

  Register /Register  

我认为您的问题可能是url模式。 更改

  Register /Register  

 

可以解决你的问题

像这样覆盖服务方法:

 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } 

还有瞧!