You are on page 1of 4

///Admin Class

public class Admin


{
private String login;
private String password;
public String getPassword()
{
return this.password;
}
public String getLogin()
{
return this.login;
}
public void setLogin(String login)
{
this.login = login;
}
public void setPassword(String password)
{
this.password = password;
}
}

///Admin DAO
public interface AdminDao
{
public boolean isValidUser(String login, String password)
throws SQLException;
}

///Admin DAO Implements


public class AdminDaoImpl implements AdminDao
{
DataSource dataSource;
public DataSource getDataSource()
{
return this.dataSource;
}
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;

}
@Override
public boolean isValidUser(String username, String password)
throws SQLException
{
String query = "Select count(1) from admin where
login = ? and password = ?";
PreparedStatement pstmt =
dataSource.getConnection().prepareStatement(query);
pstmt.setString(1, login);
pstmt.setString(2, password);
ResultSet resultSet = pstmt.executeQuery();
if (resultSet.next())
return (resultSet.getInt(1) > 0);
else
return false;
}
}

///Admin Service
public interface AdminService
{
public boolean isValidUser(String login, String password)
throws SQLException;
}

///Admin Service implements


public class AdminServiceImpl implements AdminService
{
private AdminDao adminDao;
public AdminDao getAdminDao()
{
return this.adminDao;
}
public void setAdminDao(AdminDao adminDao)
{
this.adminDao = adminDao;
}
@Override
public boolean isValidUser(String login, String password)
throws SQLException
{
return adminDao.isValidUser(login, password);
}
}

///Admin Delegate

public class AdminDelegate


{
private AdminService adminService;
public AdminService getAdminService()
{
return this.adminService;
}
public void setAdminService(AdminService adminService)
{
this.adminService = adminService;
}
public boolean isValidUser(String login, String password)
throws SQLException
{
return adminService.isValidUser(login, password);
}
}

///Admin Controller
@Controller
public class AdminController
{
@Autowired
private AdminDelegate adminDelegate;
@RequestMapping(value="/login",method=RequestMethod.GET)
public ModelAndView displayLogin(HttpServletRequest request,
HttpServletResponse response, Admin admin)
ModelAndView model = new ModelAndView("login");
//LoginBean loginBean = new LoginBean();
model.addObject("admin", admin);
return model;

}
@RequestMapping(value="/login",method=RequestMethod.POST)

public ModelAndView executeLogin(HttpServletRequest request,


HttpServletResponse response, @ModelAttribute("admin")Admin admin)
{
ModelAndView model= null;
try
{
boolean isValidUser =
AdminDelegate.isValidUser(admin.getUsername(), admin.getPassword());
if(isValidUser)
{
System.out.println("User
Login Successful");
request.setAttribute("loggedInUser", admin.getUsername());
model = new
ModelAndView("welcome");
}
else
{
model = new
ModelAndView("login");
request.setAttribute("message", "Invalid credentials!!");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return model;
}

You might also like