This is for UserLogin Form.How to check user Login form using MVC2 architecture. Here Jsp acting as a View Component and DAO act as a Model component and Servlet act as a Controller. 


View Component: 

This is mainly used for presentation purpose.

Model Component:

This is mainly used for ReUseble Component.This Model Componet is directly interact with database server and extra methods.

Controller:

This mainly used for creating a javabean object and call the method and dispatching the servlet to another resource.



Step1: create a login form name login.jsp

step2:develop a Dao and name UserDAO.java

step3: develop a servletclass name LoginServlet.java

step4:
if usernme and password is valid open the new page name userLogged.jsp

step5: if username and password is InValid open the new page name invalidLogin.jsp


login.jsp


<form action="LoginServlet">

            Please enter your username        
            <input type="text" name="un"/><br>       
       
            Please enter your password
            <input type="text" name="pw"/>
           
            <input type="submit" value="submit">           
       
        </form>


UserDAO.java

 import java.text.*;
   import java.util.*;
   import java.sql.*;

   public class UserDAO    
   {
      static Connection currentCon = null;
      static ResultSet rs = null; 
   
   
   
      public static UserBean login(UserBean bean) {
   
         //preparing some objects for connection
         Statement stmt = null;   
   
         String username = bean.getUsername();   
         String password = bean.getPassword();  
       
         String searchQuery =
               "select * from users where username='"
                        + username
                        + "' AND password='"
                        + password
                        + "'";
       
      // "System.out.println" prints in the console; Normally used to trace the process
      System.out.println("Your user name is " + username);         
      System.out.println("Your password is " + password);
      System.out.println("Query: "+searchQuery);
       
      try
      {
         //connect to DB
         currentCon = ConnectionManager.getConnection();
         stmt=currentCon.createStatement();
         rs = stmt.executeQuery(searchQuery);           
         boolean more = rs.next();
          
         // if user does not exist set the isValid variable to false
         if (!more)
         {
            System.out.println("Sorry, you are not a registered user! Please sign up first");
            bean.setValid(false);
         }
           
         //if user exists set the isValid variable to true
         else if (more)
         {
            String firstName = rs.getString("FirstName");
            String lastName = rs.getString("LastName");
            
            System.out.println("Welcome " + firstName);
            bean.setFirstName(firstName);
            bean.setLastName(lastName);
            bean.setValid(true);
         }
      }

      catch (Exception ex)
      {
         System.out.println("Log In failed: An Exception has occurred! " + ex);
      }
       
      //some exception handling
      finally
      {
         if (rs != null)    {
            try {
               rs.close();
            } catch (Exception e) {}
               rs = null;
            }
   
         if (stmt != null) {
            try {
               stmt.close();
            } catch (Exception e) {}
               stmt = null;
            }
   
         if (currentCon != null) {
            try {
               currentCon.close();
            } catch (Exception e) {
            }

            currentCon = null;
         }
      }

return bean;
   
      }   
   }


LoginServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
                       throws ServletException, java.io.IOException {

try
{       

     UserBean user = new UserBean();
     user.setUserName(request.getParameter("un"));
     user.setPassword(request.getParameter("pw"));

     user = UserDAO.login(user);
                  
     if (user.isValid())
     {
           
          HttpSession session = request.getSession(true);       
          session.setAttribute("currentSessionUser",user);
          response.sendRedirect("userLogged.jsp"); //logged-in page             
     }
           
     else
          response.sendRedirect("invalidLogin.jsp"); //error page
}
       
       
catch (Throwable theException)        
{
     System.out.println(theException);
}
       }
    }        

UserBean.java
 public class UserBean {
   
      private String username;
      private String password;
      private String firstName;
      private String lastName;
      public boolean valid;
   
   
      public String getFirstName() {
         return firstName;
    }

      public void setFirstName(String newFirstName) {
         firstName = newFirstName;
    }

   
      public String getLastName() {
         return lastName;
            }

      public void setLastName(String newLastName) {
         lastName = newLastName;
            }
           

      public String getPassword() {
         return password;
    }

      public void setPassword(String newPassword) {
         password = newPassword;
    }
   
           
      public String getUsername() {
         return username;
            }

      public void setUserName(String newUsername) {
         username = newUsername;
            }

               
      public boolean isValid() {
         return valid;
    }

      public void setValid(boolean newValid) {
         valid = newValid;
    }   
}

ConnectionManger.java


 */
package a.b;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author user
 */
public class ConnectionManager {
   
    public static Connection get(){
        Connection con=null; 
         try{
           
        Class.forName("com.mysql.jdbc.Driver");
   con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
    
       
    }catch(Exception  e){
       
    }
   return con;
}
}


4 comments:

  1. thanks for the nice example but here you have not provided two files invalidLogin and userLogged
    thats not a problem, i am trying to return the value of username on userlogged in so its not coming
    its showing null value , i m trying to pass the session but still its not working please help me out.. :- Saurabh here

    ReplyDelete
  2. Thanks.I Understood clearly

    ReplyDelete
  3. The article is so appealing. You should read this article before choosing the Big data service providers you want to learn.

    ReplyDelete
  4. Nice Blog, When I was read this blog, I learnt new things & it’s truly have well stuff related to developing technology, Thank you for sharing this blog. If Someone wants to know about Top Big Data Companies this is the Right place for you!

    ReplyDelete