Login Example: JSP and Servlets: Part III.

Hello Friends,

This is Part III of this jsp and servlet login tutorial. Check Part I and Part II here.

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.

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.

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


Nikhil Naoghare


  1. GregBrannon


    I’ve been struggling with this type of problem for days, trying to execute a simple login/user verification routine. Your example code was the simplest I’ve found that allowed me to understand and then troubleshoot the code until I could succeed.

    I’m using Eclipse/WTP/Apache Derby Plugin and Tomcat6 on a Linux platform. Following your instructions exactly, I was continually getting connection failures – first ClassNotFoundExceptions and then SQLExceptions, because the database could not be found. These are the changes I had to make to your ConnectionManager to get the code to successfully connect to the database and verify a user’s login:

    // changed from “jdbc:mysql://localhost:3306/”
    // then moved derby.jar to the /usr/share/tomcat6 directory,
    // my tomcat6 installation directory
    String url = “jdbc:derby:”;

    // had to include the full pathname to the database. i know this
    // should be simpler, but i’m tired of playing with it tonight.
    String dbName = “/home/gregb/Documents/Programming/Workspaces/javaeetutorial/LoginExample/data/code2java;”;

    // changed from “com.mysql.jdbc.Driver”
    // changed from “url+dbName,uname,pwd”
    conn = DriverManager.getConnection( url + dbName );
    } [/code]
    I’m thrilled that it now works, and I have a template to follow for similar applications I plan to write. I hope this is helpful to someone else.

    Thanks again for your time.

    1. Pask

      I just added in lib folder jstl-1.2.jar and it works perfectly :D….tnx for this tutorial

  2. michael-aes

    that was a nice tutorial for the beginners.. works like charm…..

  3. michael-aes

    tht was a nice post… charm to learn as a beginner.. works perfectly…

    1. shubham

      ya very nice …bt dont v require struts-config.xls file???

  4. celebricut.com

    Hi Sir, First of all thank u for such tutorial , I am doing some task that is based on Encryption and Decryption for secure message in jsp or servlet .


Leave a Comment

Your email address will not be published. Required fields are marked *