gpt4 book ai didi

java - 转发 servlet POST 请求时出现 HTTP 错误 405

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:39 30 4
gpt4 key购买 nike

我是一个 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 的路径。

关于java - 转发 servlet POST 请求时出现 HTTP 错误 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31827245/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com