mysql - response.sendRedirect() 显示错误

标签 mysql jsp authentication

我正在为登录编写一个简单的 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/

相关文章:

authentication - 使用JSONP和Cookie进行跨域登录

mysql - 从 codeigniter 调用存储过程不起作用

mysql - 在 MySql 中创建触发器时声明变量

php - 使用其他网络帐户登录帐户实现

javascript - Passport 认证中的独特模式

java - 如何在没有提交模式的情况下从表单发送信息?

php - 单击时为 Highcharts 生成有效的 JSON

php - Laravel - 组合多个查询以使用单个查询更新两个表的数据

java - Spring 安全: Authentication results in HTTP 405 "Method not allowed"

javascript - 如何仅获取纯文本(字符串)来响应 Ajax url?