Java 获取错误 HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET,但从未使用过“get”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23624442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:22:50  来源:igfitidea点击:

getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever?

javamysqldatabasejspservlets

提问by user3631223

I'm a beginner and making a small registration program with database But i'm trying to run this but it's giving me some errors pls help:

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

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

And here is my register.htmlcodes:

这是我的register.html代码:

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="Register" method="post">
    Name: <input type="text" name="name">
    Email: <input type="text" name="email">
    Password: <input type="password" name="password">
    Country:

    <select name="userCountry">
        <option>India</option>
        <option>Pakistan</option>
        <option>Other</option>
    </select><br><br>

    <input type="submit" value="register">
</form>
</body>
</html>

Here is my Register.javacodes:

这是我的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();
    }
}

And here is my Web.xml:

这是我的Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>Register</servlet-name>
        <servlet-class>Register</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Register</servlet-name>
        <url-pattern>/register.html</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>register.html</welcome-file>
    </welcome-file-list>
</web-app>

Help would be appreciated!!

帮助将不胜感激!

采纳答案by Leos Literak

The problem is that you mapped your servlet to /register.htmland it expects POST method, because you implemented only doPost()method. So when you open register.htmlpage, it will not open html page with the form but servlet that handles the form data.

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

Alternatively when you submit POST form to non-existing URL, web container will display 405 error (method not allowed) instead of 404 (not found).

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

To fix:

修理:

<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register</url-pattern>
</servlet-mapping>

回答by Totò

I think your issue may be in the url pattern. Changing

我认为您的问题可能出在 url 模式中。改变

<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register</url-pattern>
</servlet-mapping>

and

<form action="/Register" method="post">

may fix your problem

可能会解决您的问题

回答by Mihir Deshpande

Override service method like this:

像这样覆盖服务方法:

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

And Voila!

瞧!