title: How to write custom exceptions for login in java
body: <p>I am building a web application i ve a login jsp form which will be redirected to a servlet...</p>
  
  <p>My servlet code:</p>
  
  <pre><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);
   }
  </code></pre>
  
  <p>and from their to service class:
  My service class code:</p>
  
  <pre><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);
  
      }
      }
  }
  
  
      }
  </code></pre>
  
  <p>and from their to DAO</p>
  
  <p>My LoginDAO:</p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p>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...</p>