Login Example – JSP and Servlets: Part I.

Hello Friends,

This is one of the tutorials on JSP and Servlets. In this we will discuss and create a simple login application using servlets and JSP where we will connect to the DB for the user credentials and then authenticate the users accordingly. First of all we will discuss about what we will need for this.

JSP Pages:

We will create three JSP pages which will be the only viewable parts of our code. The three pages will be as Login Page, Login Success page and the Invalid Login page. Login page will be the welcome page for our application which will take the username and password from the user and then authenticate them. Depending on the inputs on this page we will redirect the user to either Login Success page or Invalid login page.

Servlet File:

The next thing we will need is a Servlet file “LoginServlet.java” which will be used for mapping or redirection of the JSP pages as per the user inputs and the results of the DB query. Here we will use the doPost () method in order to send the Http requests to the server.

Java Bean:

To get the user data from the JSP page and send it to the Servlet for further operations, we will need another java file, and this file will contain the Setters and Getters that will save the . This file is called as the Bean. As per the definition, Beans are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.

Data Access Objects (DAO):

As its name suggests, DAOs are used for accessing the specific data. It provides an interface to some type of database or similar mechanism, providing specific operations without exposing details of the database. The advantage of using DAOs is the separation between two important parts of any application that should know almost nothing of each. Changing business logic can rely on the same DAO interface, while changes to persistence logic do not affect DAO clients as long as the interface remains correctly implemented. In this application we will be using the LoginDAO file for accessing the data from the Database.

Database Connection Manager:

To be able to access the data of database through DAOs one must need to establish the connection to the DB so that DAOs can communicate with it. For this, we need to implement the class which will allow the secure connection between the Database and the calling application. The DAO will use the connection object of the class which is implemented in the Connection Manager class.

As of now, we are clear of what exactly we want in our application. At the end our project navigator will contain structure something like shown in the image below:

Project Navigator

In order to achieve this we will move forward step by step.

Step 1) Create a Dynamic Web Project in your Eclipse IDE and name it as LoginExample. Then add the package name come2niks under the src folder. This package will contain all the java files and also the Servlet file.

Step 2) Now we will create the first page (welcome page) of our application. Create a jsp file under the WEB Content folder, name it  as LoginPage.jsp. Add the below code in the file:

Step 3) Similarly create two jsp pages under WEB Content folder for navigating the user when login succeeds or fails. Name it as LoginFailed.jsp and LoginSuccess.jsp. Add the simple html code for now, you can change it to what you want. In this example, we have used the following code.

LoginSuccess.jsp

LoginFailed.jsp code:

At this stage we are finished with the first part of our tutorial. The remaining part of the application continues in the next blog.

Next >>

Thanks,

Nikhil Naoghare.

2 Responses to Login Example – JSP and Servlets: Part I.

  1. When I try to execute the jsp page, it does not execute the jsp and instead the servlet page is executed first… How do I solve this problem?

    • admin says:

      I think, you have not created the dynamic web page or some configuration is missing. If you are using eclipse then go to the properties of the project –>Go to Project Facets –> Check the Dynamic Web Module button, also check the version of Jre you have and what you are using in that project.

      Or, check the welcome-file list in the web.xml, add only jsp page which you need to load at start up.

Leave a Reply

%d bloggers like this: