Hi,
Below method is used to authenticate a general user and admin user, else error page.
There is some issue with the login process. This method is not working. If I remove
( if(username.equals("admin")&& password.equals("admin"))
result="admin"
) this condition, code works fine and displays the general user page. However when I include the above code as below to authenticate admin user, it does not work for any user(general or admin). Please point my mistake.
public
String login() throws SQLException
{ Connection con;
MyDAO dao=new MyDAO();
Statement st;
ResultSet rs;
String result="success";
try{
con=dao.getConnection();
st=con.createStatement();
rs=st.executeQuery("select username,password from ACC_TB");
//hardcoded admin user name and password.
if(username.equals("admin")&& password.equals("admin"))
result="admin"
while(rs.next()){
String un=rs.getString("USERNAME");
String pwd=rs.getString("PASSWORD");
if ( username.equals(un) && password.equals(pwd))
result="success";
}//end while
if((result!=("success"))||(result!=("admin")))
{
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage("Invalid Username and/or Password");
context.addMessage("loginForm", message);
result="failure";
}
} catch(SQLException e){e.printStackTrace();}
return result;
}
---------------------------------------------------------------------------
Faces-config.xml
----------------------------------------------------------------------------
<faces-config>
<navigation-rule>
<from-view-id>/pages/login.jsp</from-view-id>
<navigation-case>
<from-outcome>admin</from-outcome>
<to-view-id>/pages/greeting.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/pages/login.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/user.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>jsflogin.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Thanks
Ayub