验证用户

时间:2019-04-29 16:30:21

标签: java jsp servlets

我一直在尝试检查他们的电子邮件和密码后将登录系统的用户重定向到各自的页面。但是我不确定该编码背后的逻辑,当我尝试编码时,仅使用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;

    }

}

在检查电子邮件和密码后,我想要的是下一步检查用户数据类型并将其重定向到正确的页面

2 个答案:

答案 0 :(得分:1)

在您的loginControllerServlet.java中,将其更改为

if(theUser.getType()==p)

对此

if(theUser.getType().equals(p))

答案 1 :(得分:0)

根据您的逻辑,我认为首先,您应该将归因类型设置为int,这样拥有 buffer = stbi_load(file.c_str(), &width, &height, &bpp, 4); if(stbi_failure_reason()) std::cout << stbi_failure_reason(); Pharmacist这样的类型的机会就会减少。

然后与数据库检查进行通信是正确的,但是我不认为您的getType方法是一样的,这是我建议您使用的方法:

1st:创建一个似乎已完成的bean(对象)用户,并放置getter和setter,然后将构造函数和所有属性作为参数。例子:

pharmacist

然后您检查邮件将直接返回一个用户对象,因此您应该拥有

class User {

   private int id;
   private String mail;
   private String password;
   private int type;

   public User(){

   }

   public User(int id, String mail, String password, int type){
      this.id = id;
      this.mail = mail;
      this.password = password;
      this.type = type;
   }

   // Getters and setters


}

,然后在您的Servlet中可以对:

class userDB {

   public User login(String mail, String password){

      User user = new User();
      String query = "select * from user where mail = ? and password = ?";

      try{

         PreparedStatement prep = conn.prepareStatement(query);

         prep.setString(1,mail);
         prep.setString(2,password);

         ResultSet res = res.executeQuery();

         if(res.first){

            user = new User(res.getInt("id"),res.getString("mail"),res.getString("password"),res.getInt("type"));
            return user;
         }else{

            return null;

         }

      }catch (Exception e){

        e.printStackTrace();
        return null;

      }

   }

}
相关问题