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...