HTTP 状态 405 - 此 URL java servlet 不支持 HTTP 方法 POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4297049/
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 15:31:34  来源:igfitidea点击:

HTTP Status 405 - HTTP method POST is not supported by this URL java servlet

javaservlets

提问by sutoL

I am having trouble getting the page to work. I have my form method to post and my servlet implements doPost(). However, it keeps showing me that I am not supporting the POSTmethod.

我无法使页面正常工作。我有我的表单方法要发布,我的 servlet 实现了doPost(). 但是,它不断向我表明我不支持POST方法。

I am just trying to do a simple website and insert values into my MySQL DB.

我只是想做一个简单的网站并将值插入到我的 MySQL 数据库中。

*type Status report
message HTTP method POST is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).*

the static page:

静态页面:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
        "http://www.wapforum.org/DTD/xhtml-mobile10.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>XHTML Mobile Profile Document</title>
        <!-- 
            Change href="style.css" below to the file name and
            relative path or URL of your external style sheet.
          --> 
        <link rel="stylesheet" href="index.css" type="text/css"/>
        <!-- 
        <style> document-wide styles would go here </style>
        -->
    </head>
    <body>

        <h1> Register Here </h1>
       <form action="regSuccess.do" method = "POST">
         UserName: <input type="text" name="txtregUsername" size="15" maxlength="15">
                   <br/>
         Password: <input type="password" name="txtregPassword" size="15" 
                    maxlength="15"><br/>
         Name: <input type="text" name="txtregName" size="20" maxlength="30"><br/>
         Email: <input type="text" name="txtregEmail" size="20" maxlength="30">
                <br/><br/> 
               <input type="submit" name="btnRegister" value="Register Me"/>
        </form>
    </body>
</html>

the servlet:

小服务程序:

package core;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class handlingReg extends HttpServlet {

    //database parameters
    private static final String db_server = "localhost/";
    private static final String db_name ="bus_guide";
    private Connection con = null;

    //init of connection to dabatase
    public void init(ServletConfig config) throws ServletException {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
    catch (Exception e) {
        System.out.println("Exception in init(): unable to load JDBC DriverA");
        }
    try {
    con = DriverManager.getConnection("jdbc:mysql://"+ db_server + db_name , "root" , "");
        System.out.println("conn: "+con);
        }
    catch (Exception e) {
        System.out.println(e.getMessage());
        }
    }
    //end init()

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       //response handling

       response.setContentType("text/html");
       PrintWriter out = response.getWriter();

       //handling request
       String enteredUsername = request.getParameter("txtregUsername");
       String enteredPw = request.getParameter("txtregPassword");
       String enteredName = request.getParameter("txtregName");
       String enteredEmail = request.getParameter("txtregEmail");

        //inserting values into database
        try {
            Statement stmnt = con.createStatement();
            stmnt.executeUpdate("INSERT INTO regUsers VALUES('"+enteredUsername+"','"+enteredPw+"','"+enteredName+"','"+enteredEmail+"')");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

       //output html out.println("");
       out.println("<?xml version = \"1.0\" encoding =\"utf-8\"?>");
       out.println("<!DOCTYPE html PUBLIC \"-//WAPFORUM/DTD XHTML Mobile 1.0//EN\"");
       out.println("    \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">");
       out.println("<html xmlns=\"http://www.w3.org/1000/xhtml\">");

       out.println("<head>");
       out.println("<title></title>");
       out.println("</head>");
       out.println("<body>");
       out.println("Register Success!");
       out.println("<a href = \"index.xhtml\"> Click here to go back to main page. 
                    </a>");
       out.println("</body>");
       out.println("</html>");
    }

}

web.xml:

网页.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

 <!--Self declared servlet mapping starts here-->
 <servlet>
  <servlet-name>handleRegister</servlet-name>
  <servlet-class>core.handlingReg</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>handleRegister</servlet-name>
  <url-pattern>/regSuccess.do</url-pattern>
 </servlet-mapping>

 <!--Self declared servlet mapping ends here-->  
 <servlet-mapping>
  <servlet-name>invoker</servlet-name>
  <url-pattern>/servlet/*</url-pattern>
 </servlet-mapping>
 <mime-mapping>
  <extension>xhtml</extension>
  <mime-type>text/html</mime-type>
 </mime-mapping>
 <mime-mapping>
  <extension>jad</extension>
  <mime-type>text/vnd.sun.j2me.app-descriptor</mime-type>
 </mime-mapping>
 <mime-mapping>
  <extension>jar</extension>
  <mime-type>application/java-archive</mime-type>
 </mime-mapping>
</web-app>

edit:removed doGet(request,response);

编辑:删除 doGet(请求,响应);

采纳答案by Martin Algesten

It's because you're calling doGet()without actually implementing doGet(). It's the default implementation of doGet() that throws the error saying the method is not supported.

这是因为您在调用时doGet()并未实际实现doGet(). doGet() 的默认实现会引发错误,指出该方法不受支持。

回答by Enno Shioji

It says "POST not supported", so the request is not calling your servlet. If I were you, I will issue a GET (e.g. access using a browser) to the exact URL you are issuing your POST request, and see what you get. I bet you'll see something unexpected.

它说“不支持POST”,所以请求没有调用你的servlet。如果我是您,我将向您发出 POST 请求的确切 URL 发出 GET(例如使用浏览器访问),然后看看您得到了什么。我打赌你会看到一些意想不到的东西。

回答by kumar

if you are using tomcat you may try this

如果你使用的是 tomcat 你可以试试这个

<servlet-mapping>

    <http-method>POST</http-method>

</servlet-mapping>

in addition to <servlet-name>and <url-mapping>

除了<servlet-name><url-mapping>

回答by Gabriel Amazonas Mesquita

This happened to me when:

这发生在我身上:

  • Even with my servlet having only the method "doPost"
  • And the form method="POST"

  • I tried to access the action using the URL directly, without using the form submitt. Since the default method for the URL is the doGet method, when you don't use the form submit, you'll see @ your console the http 405 error.

  • 即使我的 servlet 只有方法“doPost”
  • 和表单方法=“POST”

  • 我尝试直接使用 URL 访问操作,而不使用提交的表单。由于 URL 的默认方法是 doGet 方法,当您不使用表单提交时,您将看到 @您的控制台 http 405 错误。

Solution: Use only the form button you mapped to your servlet action.

解决方案:仅使用映射到 servlet 操作的表单按钮。

回答by Barefooter

If you're still facing the issue even after replacing doGet()with doPost()and changing the form method="post". Try clearing the cache of the browser or hit the URL in another browser or incognito/private mode. It may works!

如果你仍然面临的问题,即使更换后doGet()doPost()和改变形式method="post"。尝试清除浏览器的缓存或在其他浏览器或隐身/隐私模式中点击 URL。它可能有效!

For best practices, please follow this link. https://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html

如需最佳实践,请点击此链接。 https://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html