[英]Tomcat Servlet returns Error 405 - HTTP method POST is not supported by this URL
[英]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=" " 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=" " 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”。
解:
doPost
方法的簽名。 (例外情況也需要修復!) @Override
批注。 @Override
的習慣...,以便Java編譯器可以向您指出錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.