获取错误HTTP状态405 - HTTP方法GET不受此URL支持,但没有使用`get`? [英] getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever?

查看:14277
本文介绍了获取错误HTTP状态405 - HTTP方法GET不受此URL支持,但没有使用`get`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,并做一个小的注册程序与数据库但我试图运行这,但它给我一些错误pls帮助:

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

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

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

帮助将不胜感激。

推荐答案

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

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

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

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

修复:

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

这篇关于获取错误HTTP状态405 - HTTP方法GET不受此URL支持,但没有使用`get`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆