gpt4 book ai didi

java - 根据登录的用户将JSP页面重定向到另一个JSP页面

转载 作者:行者123 更新时间:2023-11-29 19:55:52 25 4
gpt4 key购买 nike

我有一个包含用户名、密码和登录按钮的登录表单。

我有一个 MySQL 数据库,它有一个登录表,包含用户名和密码。

使用登录Servlet,我试图实现以下目标:

  • 检查用户名和密码是否与登录名中存储的内容匹配表。
  • 如果匹配,则检查该用户是谁并将其重定向到他的 JSP 页面。

但是,我的下面的代码不起作用:

登录表:

enter image description here

lgoin Servlet try-catch 代码:

 String userName_Login = request.getParameter("Username");
String passWord_Login = request.getParameter("Password");
String DB_URL = "jdbc:mysql://localhost:3306/evaluationDB";
String DB_UNAME = "root";
String DB_PASS = "******";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, DB_UNAME, DB_PASS);
Statement stmt = conn.createStatement();
//String loginSQL = "SELECT userName, passWord FROM login";
String loginSQL = "SELECT * from login";
ResultSet rs = stmt.executeQuery(loginSQL);

while(rs.next()){
ArrayList<String> userNameArray = new ArrayList<String>();
ArrayList<String> passWordArray = new ArrayList<String>();

userNameArray.add(rs.getString("userName"));
passWordArray.add(rs.getString("passWord"));

if (rs.getString("userName").equals(userName_Login) && rs.getString("passWord").equals(passWord_Login)){
for (int index = 0 ; index < userNameArray.size() ; index++){
switch (userNameArray.get(index)){
case "SystemManager":
response.sendRedirect("SystemManager.jsp");
System.out.println("Welcome SystemManager ");
break;
case "Secretary":
response.sendRedirect("Secretary.jsp");
System.out.println("Welcome Secretary ");
break;

case "TeamLeader":
response.sendRedirect("TeamLeader.jsp");
System.out.println("Welcome TeamLeader ");
break;

case "WorkOrganizerLeader":
response.sendRedirect("WorkOrganizerLeader.jsp");
System.out.println("Welcome WorkOrganizerLeader ");
break;

case "ProjectManager":
response.sendRedirect("ProjectManager.jsp");
System.out.println("Welcome ProjectManager ");
break;

default :
System.out.println("I don't know you ... ");

}
}

}
else{
System.out.println("Get Out ... : ");
break;
}

}
rs.close();
stmt.close();
conn.close();
}


catch (ClassNotFoundException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

但它不起作用,switch case 打印所有 case 并打开所有 jsp 页面。

感谢您的帮助。

最佳答案

将您的 LoginSQL 查询更改为下面的内容,以获取包含用户输入的用户名和密码的记录。

String loginSQL = "SELECT * from login where userNameArray   
='"+userName_Login+"' and passWord ='"+passWord_Login+"'";

更新您的代码以删除 while 循环和 if 语句,因为 for 循环中的逻辑适用于所有用户,如下所示:

    ResultSet rs = stmt.executeQuery(loginSQL);
if(rs.next()){
//If record exists as per updated query
String mycase=rs.getString("userName");
switch (mycase){//fetch Username
case "SystemManager":
response.sendRedirect("SystemManager.jsp");
System.out.println("Welcome SystemManager ");
break;
case "Secretary":
response.sendRedirect("Secretary.jsp");
System.out.println("Welcome Secretary ");
break;

case "TeamLeader":
response.sendRedirect("TeamLeader.jsp");
System.out.println("Welcome TeamLeader ");
break;

case "WorkOrganizerLeader":
response.sendRedirect("WorkOrganizerLeader.jsp");
System.out.println("Welcome WorkOrganizerLeader ");
break;

case "ProjectManager":
response.sendRedirect("ProjectManager.jsp");
System.out.println("Welcome ProjectManager ");
break;

default :
System.out.println("I don't know you ... ");

}
}
else{
System.out.println("Get Out ... : ");
}
rs.close();
stmt.close();
conn.close();

关于java - 根据登录的用户将JSP页面重定向到另一个JSP页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711138/

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