简体   繁体   English

tomcat说此URL不支持http方法发布

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

i've been calling servlet by a HTML page and my servlet code goes like this: 我一直在通过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();
    }
}

and my html page is this: 和我的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>

but the problem is that whenever i run all this, the application server says, that POST method isn't supported by this url. 但是问题是,每当我运行所有这些程序时,应用程序服务器就会说,此URL不支持POST方法。 I've experienced this error frequently, please explain why all this happens. 我经常遇到此错误,请解释为什么发生所有这些错误。 I've mapped the servlet in my web.xml 我已经将servlet映射到了web.xml中

Thanks in advance. 提前致谢。

You have made a mistake in your doPost method. 您在doPost方法中犯了一个错误。 You have declared it as this: 您已声明为:

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

but the correct signature is this: 但是正确的签名是这样的:

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

Notice the different parameter order .... 注意不同的参数顺序...。

Since your method doesn't have the correct signature, it is not overriding the inherited version. 由于您的方法没有正确的签名,因此它不会覆盖继承的版本。 That means that your version never gets called. 这意味着您的版本永远不会被调用。 Instead, a POST requests calls the inherited method ... and the behavior of that is to say "POST not supported". 而是,一个POST请求调用继承的方法...,并且该行为表示“不支持POST”。

Solution: 解:

  1. Correct your doPost method's signature. 更正doPost方法的签名。 (The exceptions will need fixing too!) (例外情况也需要修复!)
  2. Add an @Override annotation to this ... and any other override methods in this class. 向此...和此类中的所有其他覆盖方法添加@Override批注。
  3. Get into the habit of always using @Override wherever you intend to override a method ... so that the Java compiler can point out your mistakes to you. 养成在打算覆盖方法的任何地方都始终使用@Override的习惯...,以便Java编译器可以向您指出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM