0

I am building a web application i ve a login jsp form which will be redirected to a servlet...

My servlet code:

Login login=new Login();
        login.setUserName(request.getParameter("txtUsername"));
        login.setPassWord(request.getParameter("txtPassword"));

        LoginService ls=new LoginService();
        ls.loginValidate(login);
        RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
        rd.forward(request, response);
 }

and from their to service class: My service class code:

public class LoginService {
    Login login=null;

    public Login loginValidate(Login login) {
        validateLogin(login);
        return login;
    }

    private void validateLogin(Login login) {
        this.login=login;
         if(login!=null){
       LoginDAO ld=new LoginDAO();
       DataSource dataSource=new DataSource();
       ld.setDataSource(dataSource);
        ld.validate(login);

    }
    }
}


    }

and from their to DAO

My LoginDAO:

public class LoginDAO {
    private DataSource dataSource;
    public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    }

    public  boolean validate(Login login){
        boolean status=true;
        String sql="select * from Login where UserName=? and PassWord=?";

        Connection conn = null;
        try{
        conn = dataSource.createConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,login.getUserName());
        ps.setString(2,login.getPassWord());
        ResultSet rs=ps.executeQuery();
        status=rs.next();
        }

        catch (SQLException e) {
            throw new RuntimeException(e);

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {}
            }
        }
        return status;
}
}

But when i type the correct username and password its redirecting to success page(Login.jsp in this context) and even if i type wrong un and pw its redirecting i do understand why because after method call in servlet i have dispatched it to login.jsp so in any condition it will redirect me. But one of my friend to resolve this suggested me to add custom exception class when the un and pw are typed wrongly.. now i wanna know how to write this custom exception class for this app. So please some one help me in this regard... Thanks in advance...

kryger
  • 12,906
  • 8
  • 44
  • 65
user3222718
  • 242
  • 1
  • 7
  • 27

2 Answers2

0
try{
    boolean status=true;
    stmt=con.createStatement();
    res=stmt.executeQuery("select count(*) from user where USER_NAME='"+strUsername+"'and PASSWORD='"+strPassword+"'");
    res.next();
    count=res.getInt(1);
    if(count==1){
      return status;
    }
    else{
       return false;
    }
}
  • query execute zero result
  • your status is set as true
  • so it return true always
sarath
  • 767
  • 12
  • 19
0

ls.loginValidate(login) can return login status like true or false boolean or logged in user object if login successfull. depends on the return type of loginVlidate you can set message to jsp pag like:

    if(login){
    setAttribute("LoginMsg","Login Successfull.")
    RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
    rd.forward(request, response);
    }else{
    setAttribute("LoginMsg","Login fails.")
    RequestDispatcher rd=request.getRequestDispatcher("may be another page    (error)");
    rd.forward(request, response);
    }

After you can get that attriubte in jsp.

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
  • if(login) is showing an error type mismatch cannot convert from login to boolean as if clause can take only boolean expressions – user3222718 Feb 12 '14 at 07:11
  • I have given the example only. You could return boolean from loginValidate method if you want to return object of found user you can check by object if it is null or not in if else.. – Shekhar Khairnar Feb 12 '14 at 07:16