一尘不染

收到错误HTTP状态405-此URL不支持HTTP方法GET,但从未使用过`get`吗?

jsp

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

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代码:

<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>

这是我的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

<?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>

帮助将不胜感激!


阅读 3188

收藏
2020-06-08

共1个答案

一尘不染

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

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

修理:

<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register</url-pattern>
</servlet-mapping>
2020-06-08