【问题标题】:tomcat says http method post is not supported by this urltomcat 说这个 url 不支持 http 方法 post
【发布时间】:2019-04-07 22:27:42
【问题描述】:

我一直在通过 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 方法。 我经常遇到这个错误,请解释为什么会发生这一切。 我已经在我的 web.xml 中映射了 servlet

提前致谢。

【问题讨论】:

    标签: java html tomcat web


    【解决方案1】:

    您在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 编译器就可以向您指出错误。

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 2020-08-31
      • 2016-07-02
      • 2014-08-05
      • 2017-01-28
      • 2018-04-03
      • 2020-09-17
      • 1970-01-01
      相关资源
      最近更新 更多