繁体   English   中英

具有角色的Java Servlet登录

[英]java servlet login with role

我从Java开始,并且很难实现登录。 我希望我的servlet根据角色(已在数据库中注册)以不同的方式重定向用户,我尝试了许多不同的版本,但我进行了最后一次尝试。 我不明白为什么不起作用(如果我以管理员身份登录,则在页面上为用户重定向servlet)是否还有其他错误(也很合理),请告诉我。 我想改善。 谢谢大家的回答。

我的DAO:

public String esisteAccount (Account a) {
        Account acc = new Account();
        String query = "SELECT * FROM account WHERE username = '?' and password='?'";
        query=query.replaceFirst("[?]", (a.getUsername()));
        query=query.replaceFirst("[?]",(a.getPassword()));
        Vector<Object> v =db.executeSelect(query, "Account");
        acc = (Account)v.get(0);
        String ruolo;
        if (v.size()>0){
            ruolo=acc.getRuolo();
            }
        else { 
            ruolo="nonAutorizzato";
            }
        return ruolo;}

我的dbManager

if ( type.equals("Account") )
                {
                    Account a;
                    a = new Account();
                    a.setUsername( rs.getString("username") );
                    a.setPassword( rs.getString("password") );
                    a.setRuolo( rs.getString("ruolo") );
                    v.add(a);
                }

和我的servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Account a = new Account();
        a.setUsername (username);
        a.setPassword (password);
        AccountDAO accountDAO =new AccountDAO();
        String esito= accountDAO.esisteAccount(a);
        if (esito=="nonAutorizzato"){       
            request.getRequestDispatcher("../errore.jsp").forward(request, response);
        }
        else {
            HttpSession session=request.getSession();
            if (esito=="Admin"){
                session.setAttribute("autorizzatoAdmin","true");
                request.getRequestDispatcher("autorizzatoAdmin.jsp").forward(request, response);
            }
            else{
                session.setAttribute("autorizzato","true");
                request.getRequestDispatcher("indexPL.jsp").forward(request, response);
            }
        }

这是问题

if (esito=="Admin")

用算术运算符检查两个字符串不正确。 而不是像这样使用。

if (esito.equalsIgnoreCase("Admin"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM