Login Web Application using Java Mysql Servlet Part-2

Login Web Application using Java Mysql Servlet Part-2

This is the second part of the series. If you have missed the first part, check it out Login Web Application using java – part1

In this we are going to design a complete Login page using Java and Servlet with validation. If the user user already registered [refer part1] then user can  login.

When user submits the data, we check whether user exists with particular mailid. After the user is redirected to home page.

1) Login Page

When user submits, data is passed to the Login Servlet

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/login" method="post">
	<table border="0">
	<tr>
		<th> Login Page </th></tr>
	<tr>
		<td>Email</td>
		<td> <input type="email" name="mailid" value="" placeholder="enter mailid" required></td>
	</tr>
	<tr>
		<td>Password</td>
		<td> <input type="password" name="password" value="" placeholder="enter password" required></td>
	</tr>
	<tr><td colspan="1" align="center"><input type="submit" value="submit"></td></tr>
	</table>
</form>
</body>
</html>

2) LoginServlet

We receive the data and Process the data in this Servlet. If the user registration is successful, he gets redirected to Home.jsp Session is created once loggin is successful.

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/login")
public class LoginServlet extends HttpServlet 
{
	
	private LoginDAO loginDao;
	public void init()
	{
		loginDao = new LoginDAO();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String mailid = request.getParameter("mailid");
		String password = request.getParameter("password");
		//TODO regex validation for input
		//TODO password salting/encryption
		LoginBean loginBean = new LoginBean();
		loginBean.setmailid(mailid);
		loginBean.setPassword(password);
		try {
			boolean success = loginDao.validate(loginBean);
			if(success)
			{
				response.sendRedirect("Home.jsp");
				HttpSession session = request.getSession();
		        session.setAttribute("mailid", mailid);
			}
			else
			{
				 PrintWriter out = response.getWriter();
				 out.println("You have entered wrong mailid or password");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
}

3)LoginBean

public class LoginBean {
	private String mailid;
	private String password;
	
	public void setmailid(String mailid)
	{
		this.mailid = mailid;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	public String getmailid()
	{
		return mailid;
	}
	public String getPassword()
	{
		return password;
	}

}

4) LoginDAO (DataAccessObject)

The Data Access Object is basically an object or an interface that provides access to an underlying database. It checks whether the user is present in database or not.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.db.*;
public class LoginDAO {
	public boolean validate(LoginBean loginBean) throws Exception
	{
		DBConnection dbconnect = new DBConnection();
		Connection connection = dbconnect.connect();
		PreparedStatement preparedStatement = connection.prepareStatement("Select * from Users where mailid = ? and password = ?");
		preparedStatement.setString(1, loginBean.getmailid());
		preparedStatement.setString(2, loginBean.getPassword());
		ResultSet rs = preparedStatement.executeQuery();
		boolean isRowPresent = rs.next();
		return isRowPresent;
	}    
}

Home.jsp

This is the Home page of our Web application. User can logout if needed.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
    <body>
        <%
        String email=(String)session.getAttribute("mailid");
        
        //redirect user to login page if not logged in
        if(email==null){
        	response.sendRedirect("index.html");
        }
        %>
        <p>Welcome <%=email%></p>    
        <a href="logout.jsp">Logout</a>
    </body>
</html>

Logut.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
session.invalidate();
response.sendRedirect("index.html");
%>

Login Web Application using java Screenshots

Login Page
Wrong password
Login

Check my Github Repository for full code

Sree Hari Sanjeev

The founder and CEO of Wisdom Overflow. His enthusiasm and effort has taken the blog to next level. You will be motivated just by taking a look at his daily schedule.

Leave a Reply

Your email address will not be published.

twenty + 8 =