Skip to main content

Login Application using MVC design pattern

MVC stands for Model View and Controller.

MVC is a Design pattern which separate business logic and Presentation logic.

Controller accepts the user request and Transfer request to who ever can handle it.

Model represents data transfer between layer.

View is for presentation.

Advantage of MVC Architecture


Easy to maintain the large application.

Login Application using MVC design Pattern.


Controller(servlet) accepts the request from browser and transfer to DAO(data access object ) class.
DAO class verifies the username and password in database.

mvc-architecture

Login application using MVC architecture.


project_strcucture

  • login.html
  • Loginservlet.java (Controller)
  • User_info.java (Model)
  • User_DAO.java

Login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html>
 
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
 
<body>
<form action="LoginServlet" method="post">
  <fieldset>
 
    <legend>Personal information:</legend>
 
    User name:<br>
    <input type="text" name="user_name">
    <br>
 
    Password:<br>
    <input type="password" name="password">
    <br><br>
 
    <input type="submit" value="Submit">
 
   </fieldset>
 
</form>
</body>
</html>

User_info.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.spy.javaforlearn;
 
public class User_info
{
 
	String username;
	String password;
 
	public String getUsername() 
	{
		return username;
	}
 
	public void setUsername(String username) 
	{
		this.username = username;
	}
 
	public String getPassword() 
	{
		return password;
	}
 
	public void setPassword(String password) 
	{
		this.password = password;
	}
}

Loginservlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.spy.javaforlearn;
 
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;
 
 
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
 
 
 
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
	{
 
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
 
		out.print("Get is called");
 
	}
 
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
 
		String username=request.getParameter("user_name");
		String password=request.getParameter("password");
 
		System.out.println("username is: "+username);
		System.out.println("password is: "+password);
 
 
		User_info user=new User_info();
 
		user.setUsername(username);
		user.setPassword(password);
 
 
		User_DAO  dao=new User_DAO();
 
        boolean result=dao.authentication(user);
 
 
	    if(result)
	    {
 
	    	out.println("Login Successfull");
 
 
	    }
	    else
	    {
	    	out.println("Wrong credential...");
	    }
 
	}
 
}

User_DAO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.spy.javaforlearn;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class User_DAO 
{
 
    boolean result;
 
	public boolean authentication(User_info Obj)
	{
 
		Connection con=null;
		PreparedStatement p =null;
 
 
		try 
		{
 
 
			String username=Obj.getUsername();
			String password=Obj.getPassword();
 
			System.out.println(username);
			System.out.println(password);
 
			//Load the driver class
                         Class.forName("oracle.jdbc.driver.OracleDriver");
 
 
			 con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "sys as SYSDBA","Blakhani2012");
 
 
			 String q ="select *from password_info where username=? and password =?";
 
			  p= con.prepareStatement(q);
 
			  p.setString(1,username);
			  p.setString(2,password);
 
			  ResultSet rs=p.executeQuery();
 
 
 
	         	if(rs.next())
 
		  	  {
 
			    result= true;
			    System.out.println("Login success");
 
			   }
 
		  	 else
		            {
 
				result= false;
 
				System.out.println("username and password is not valid");
 
			    }
 
 
		}
 
 
		catch (Exception e)
	    {
 
			e.printStackTrace();
		}
 
 
 
 
			finally
			{
 
				try {
					if(con!=null)
					{
						con.close();
					}
					if(p!=null)
					{
						p.close();
					}
 
				}
 
				catch(Exception e) 
				{
 
					e.printStackTrace();
				}
 
			}
 
		return result;
 
	}	
 
}

output:

Database Table:
password_info


Login.html
login


login-success

Console output:


console