You can accomplish your goal with Apache Shiro. This project makes it easy to implement the login mechanism that you are looking for. The "Website Login Demo" link below shows a complete demonstration. The project code offers an example as well.
See: