Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have taken name and password form user and compared it with my database if they match status is true if not its false.There is an additional column of role that the entered name can be user or admin if its admin I want some other page to open if it is user i want some other page.How should i do it. The following code checks admin since i have typed admin in the insert query what change or additional code i should write? There are 3 columns in my database(user,password,role).If the username entered is in same row as admin (some different page1.jsp should open) and if it is in same row as user (some different page2.jsp should open).

 String name=request.getParameter("name");
            String password=request.getParameter("password");
            boolean status=false;
    try{
        Connection con=ConnectionProvider.getCon();
        String sql="select * from roles where name='" + name + "' and pass='" + password + "' and role='admin'";
        Statement stmt =con.createStatement();

        ResultSet rs=stmt.executeQuery(sql);
        if(rs.next())
        {
            status=true;
        }
    }catch(Exception e){}

if(status){
                out.print("Welcome, "+name);
                HttpSession session=request.getSession();
                session.setAttribute("name",name);
                request.getRequestDispatcher("create.html").forward(request, response);
                 //request.getRequestDispatcher("department.html").forward(request, response);
            }
            else{
                out.print("Sorry, username or password error!");
                request.getRequestDispatcher("login.html").include(request, response);
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

String name=request.getParameter("name");
        String password=request.getParameter("password");
        boolean status=false;
String role = "";
try{
    Connection con=ConnectionProvider.getCon();
    String sql="select * from roles where name='" + name + "' and pass='" + password";
    Statement stmt =con.createStatement();
    role ="admin";
    ResultSet rs=stmt.executeQuery(sql);
    if(rs.next())
    {
        status=true;
        role=rs.getString("role");
    }
}catch(Exception e){}

if(status){
            out.print("Welcome, "+name);
            HttpSession session=request.getSession();
            session.setAttribute("name",name);
            if(role!=null && role.equals("admin") ){            
            request.getRequestDispatcher("create.html").forward(request, response);
             //request.getRequestDispatcher("department.html").forward(request, response);
            }else{
             request.getRequestDispatcher("   <SomeOther>.html").forward(request, response);
             //request.getRequestDispatcher("department.html").forward(request, response);
          }

        }
        else{
            out.print("Sorry, username or password error!");
            request.getRequestDispatcher("login.html").include(request, response);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...