![](/img/trans.png)
[英]Tomcat Servlet returns Error 405 - HTTP method POST is not supported by this URL
[英]HTTP error 405 when forwarding servlet POST request
我是Java新手,所以請耐心等待。 我制作了與數據庫中的密碼和用戶名匹配的程序。 如果我輸入正確的密碼,則會彈出
HTTP Status 405 - HTTP method POST is not supported by this URL
--------------------------------------------------------------------------------
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).
--------------------------------------------------------------------------------
Apache Tomcat/6.0.35
但是,如果我輸入了錯誤的密碼,則會出現預期的輸出(即導航到www.google.com)。 我找不到錯誤。 我的代碼是:
package httpserv1;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Httpserv extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String name = request.getParameter("name1");
String password = request.getParameter("password1");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String userName = "sa";
String password2 = "iyoginet$123";
String url = "jdbc:sqlserver://10.1.36.15:8307;databaseName=master";
Connection con = DriverManager.getConnection(url, userName, password2);
Statement stmt = (Statement) con.createStatement();
ResultSet rs = ((java.sql.Statement) stmt)
.executeQuery("SELECT PASSWORD FROM LOGON WHERE NAME='" + name + "'");
while (rs.next()) {
String pass = rs.getString(1);
if (password.equalsIgnoreCase(pass)) {
RequestDispatcher rd = request.getRequestDispatcher("Serv");
rd.forward(request, response);
} else {
response.sendRedirect("http://www.google.co.in");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void destroy() {
System.out.println("Good Bye ! !");
}
}
根據我的代碼,如果我輸入了錯誤的密碼,它應該被轉發到另一個servlet(即serv)。
您的問題是,您正在將完全相同的請求(即POST)轉發到另一個Servlet ("Serv")
。 我假設您的第二個Servlet ("Serv")
無法處理POST請求(因為它沒有doPost-Method)。 您可能應該使用:
response.sendRedirect("/Serv");
將"/Serv"
替換為servlet
的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.