在检查其电子邮件和密码后,我一直尝试将已登录系统的用户重定向到各自的页面。但我不确定该编码背后的逻辑,当我尝试它时,它只是用 else 语句进行响应。我已经尝试过验证电子邮件和密码,效果很好并重定向到正确的页面,但是当我添加用户类型条件时,它不起作用
我尝试过包含嵌套的 if 语句,但我不确定它的逻辑,它总是执行 else 语句。
loginControllerServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email=request.getParameter("email");
String password=request.getParameter("pwrd");
User theUser=loginDbUtil.gettype(email);
if(loginDbUtil.check(email, password))
{
String p="pharmacist";
if(theUser.getType()==p)
{
// HttpSession session=request.getSession();
// session.setAttribute("email", email);
response.sendRedirect("medicine.jsp");
}
else
{
response.sendRedirect("index.jsp");
}
}else
{
response.sendRedirect("index.jsp");
}
}
}
loginDbUtil.java
public boolean check(String email,String password)
{
Connection myConn=null;
PreparedStatement myStmt=null;
ResultSet rs=null;
try
{
//get db connection
myConn=dataSource.getConnection();
//sql statemtn
String sql="select email,pass from usertab where email=? and pass=? ";
myStmt=myConn.prepareStatement(sql);
//set the param values for user
myStmt.setString(1, email);
myStmt.setString(2, password);
rs=myStmt.executeQuery();
if(rs.next())
{
return true;
}
}catch(Exception e)
{
e.printStackTrace();
}
return false;
}
public User gettype(String email) {
User type=null;
Connection myConn=null;
PreparedStatement myStmt=null;
ResultSet rs=null;
try
{
//get db connection
myConn=dataSource.getConnection();
//sql statemtn
String sql="select type from usertab where email=? ";
myStmt=myConn.prepareStatement(sql);
//set the param values for user
myStmt.setString(1, email);
rs=myStmt.executeQuery();
if(rs.next())
{
String t=rs.getString("type");
type =new User(t);
}
}catch(Exception e)
{
e.printStackTrace();
}
return type;
}
}
我想要的是在检查电子邮件和密码后,然后检查用户数据类型并将其重定向到正确的页面
最佳答案
在你的loginControllerServlet.java中将其更改为
if(theUser.getType()==p)
到此
if(theUser.getType().equals(p))
关于java - 验证用户后根据用户角色重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55907431/