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.
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.
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.
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:
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:
<%@ page language="java" contentType="text/html; charset=windows-1256" pageEncoding="windows-1256" %>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Login Page</title> </head>
<form name="actionForm" action="LoginServlet" method ="GET">
<tr><td>Enter your Username: </td><td><input type="text" name="uname"/></td></tr>
<tr><td>Enter your Password: </td><td><input type="password" name="password"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="submit"> </td></tr>
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.
User Logged Successfully
<title>Invalid Login </title>
At this stage we are finished with the first part of our tutorial. The remaining part of the application continues in the next blog.