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

问题描述 投票:17回答:5

我无法让页面工作。我有我的表单方法发布和我的servlet实现doPost()。但是,它一直向我显示我不支持POST方法。

我只是想做一个简单的网站,并将值插入我的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).*

静态页面:

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

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>");
    }

}

veb.hml:

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

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

java servlets
5个回答
22
投票

这是因为你在没有实际执行doGet()的情况下调用doGet()。这是doGet()的默认实现,它会抛出错误,说明该方法不受支持。


10
投票

如果您使用的是tomcat,可以尝试一下

<servlet-mapping>

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

</servlet-mapping>

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


3
投票

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


1
投票

这件事发生在我身上:

  • 即使我的servlet只有方法“doPost”
  • 而form方法=“POST”
  • 我尝试直接使用URL访问操作,而不使用表单提交。由于URL的默认方法是doGet方法,因此当您不使用表单提交时,您将看到@ your console的http 405错误。

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


0
投票

即使用doGet()替换doPost()并改变形式method="post",你仍然面临着这个问题。尝试清除浏览器的缓存或在其他浏览器或隐身/私密模式下点击URL。它可能有效!

有关最佳做法,请点击此链接。 https://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html

© www.soinside.com 2019 - 2024. All rights reserved.