Step 8 ) We are done with the DAOs and Beans. Now we need to create a database from where we will get the authentication details like Username and Password. So, create a database named code2java and therein add a new Table called users. Please take care of the names you provide to table columns.
Step 9) Once you are done with the Database creation, we are only left with the Connection to it. So, we wil create a new java class for the Connection Manager which will provide the connection object to the DAO.
Under the package code2java, create a java class and name it as ConnectionManager and add the below code to it.
public class ConnectionManager
static Connection conn;
public static Connection getConnection()
String url = "jdbc:mysql://localhost:3306/";
String dbName ="code2java"; // here "code2java" is the name of Database.
String uname = "root";
String pwd = "root";
conn = DriverManager.getConnection(url+dbName,uname,pwd);
catch (SQLException ex)
It will connect to the requested database and return the connection object. Here the database we will use is code2java, which we have created in the above step. Let’s assume the username and password to be “root”.
NOTE: Don’t forget to add the mysql-connection.jar file in the build path of this project.
Step 10) The most important file to be added is the web.xml which defines the redirection of the servlets. Add an xml file under the lib folder in WEB-INF folder, name it as web.xml and add the below code.
<?xml version="1.0" encoding="UTF-8"?>
Here, we have defined the url pattern for running the application. At this stage your application is ready to run. Right click on the LoginPage.jsp and click on Run As –> Run On Server. Eclipse will run the application with the defined server or will ask you to define the one. Once the application starts execution you will first see the LoginPage:
Now you can test your application. Once you enter the details you will be navigated to the LoginSuccess page if the credentials you entered matched with the one stored in DB or else you will be navigated to LoginFailed page. Try this example and let me know if you face any difficulties in this.
You can download complete code and folder structure here LoginExample