簡體   English   中英

tomcat說此URL不支持http方法發布

[英]tomcat says http method post is not supported by this url

我一直在通過HTML頁面調用servlet,而我的servlet代碼如下所示:

import java.sql.*;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

public class validation extends HttpServlet
{
    static PrintWriter pw = null;
    public void doPost(HttpServletResponse response, HttpServletRequest request) throws ClassNotFoundException, SQLException, IOException, ServletException
    {
        pw = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        RequestDispatcher rd = request.getRequestDispatcher("new.html");
        Class.forName("java.sql.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/timetabledb", "root","`");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select password from users where username = '"+username+"';");
        if(rs.next()==false)
        {
            pw.println("No such user found!");
        }
        else
        {
            if(password.equals(rs.getString("password")))
            {
                rd.forward(request, response);
            }
            else
            {
                pw.println("Invalid credentials!");
            }
        }
        rs.close();
    }
}

和我的HTML頁面是這樣的:

<!DOCTYPE html>
<html>
<head>
  <title>Login Page - SGMS</title>
  <link rel="stylesheet" href="main.css" />
</head>

<body>
    <div id = "container">
        <div class = "welcome-head">
            Welcome
        </div>
        <div class = "sw-head">
            Semi-Automatic Schedule Generator & Maintenance Software
        </div>
        <span class="logo">
            <img src="logo.gif" alt="Logo"/>
        </span>
        <div class = "form">
            <form method="POST" action="validation">
                <label for="inp-usr" class="inp">
                <input type="text" name="username" id="inp-usr" placeholder="&nbsp;" required="required">
                  <span class="label">Username</span>
                  <span class="border"></span>
                </label>
                <br>
                <label for="inp-pwd" class="inp">
                <input type="password" name="password" id="inp-pwd" placeholder="&nbsp;" required="required">
                  <span class="label">Password</span>
                  <span class="border"></span>
                </label>
                <br><br><br>
                <button class="validate-btn" onclick="show();">
                    Validate
                </button>
            </form>
        </div>
    </div>
</body>

</html>

但是問題是,每當我運行所有這些程序時,應用程序服務器就會說,此URL不支持POST方法。 我經常遇到此錯誤,請解釋為什么發生所有這些錯誤。 我已經將servlet映射到了web.xml中

提前致謝。

您在doPost方法中犯了一個錯誤。 您已聲明為:

void doPost(HttpServletResponse response, HttpServletRequest request)
    throws ClassNotFoundException, SQLException, IOException, ServletException

但是正確的簽名是這樣的:

void doPost(HttpServletRequest req, HttpServletResponse resp) 
    throws ServletException, IOException

注意不同的參數順序...。

由於您的方法沒有正確的簽名,因此它不會覆蓋繼承的版本。 這意味着您的版本永遠不會被調用。 而是,一個POST請求調用繼承的方法...,並且該行為表示“不支持POST”。

解:

  1. 更正doPost方法的簽名。 (例外情況也需要修復!)
  2. 向此...和此類中的所有其他覆蓋方法添加@Override批注。
  3. 養成在打算覆蓋方法的任何地方都始終使用@Override的習慣...,以便Java編譯器可以向您指出錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM