gpt4 book ai didi

mysql - response.sendRedirect() 显示错误

转载 作者:行者123 更新时间:2023-11-29 06:39:40 25 4
gpt4 key购买 nike

我正在为登录编写一个简单的 jsp 代码,它从 mysql 检索数据。代码在 response.sendRedirect("feedback.jsp"); 处显示错误;

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>

<%
String user=request.getParameter("username");
String passwrd=request.getParameter("password");

try{

Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/feedback", "root", "");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select username,password from users");
while(rs.next())
{
String username=rs.getString(1);
String password=rs.getString(2);
if(user.equals(username) && passwrd.equals(password))

{
response.sendRedirect("feedback.jsp");
}
else
{
out.println("Invalid Credentials");
}
}
}
catch(NumberFormatException e){
System.out.print(e);
e.printStackTrace();

}

%>

如果输入了不正确的用户名和密码组合,则与数据库的连接正在运行,因为“无效凭据”的 else 语句正在运行。我收到的错误是“响应提交后无法调用 sendRedirect()”谢谢。

最佳答案

  1. 如果 URL 是绝对的 http://www.google.com ,它重定向到 http://www.google.com .
  2. 如果 URL 不是绝对的,它会重定向 相对于当前 URL。如果 URL 以/开头,则重定向 相对于上下文根,否则它重定向到当前 url

你应该在重定向后返回。

response.sendRedirect("http://www.google.com");
return;

调用sendRedirect()后不会自动返回。

关于mysql - response.sendRedirect() 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22269036/

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