1
00:00:00,005 --> 00:00:03,004
- [Instructor] As login use case is an included use case

2
00:00:03,004 --> 00:00:06,005
on which the other functionalities depend upon,

3
00:00:06,005 --> 00:00:09,001
let us first design login functionality.

4
00:00:09,001 --> 00:00:12,007
To start with, let us take a look at its UI mockup.

5
00:00:12,007 --> 00:00:15,004
This one is simple, we start off with a page

6
00:00:15,004 --> 00:00:17,003
in which there are just these links,

7
00:00:17,003 --> 00:00:19,004
with one of them being login.

8
00:00:19,004 --> 00:00:22,003
Once we choose that, it takes us to login page

9
00:00:22,003 --> 00:00:25,002
where we enter username and password,

10
00:00:25,002 --> 00:00:29,004
and then once the login is successful, it shows other links,

11
00:00:29,004 --> 00:00:33,009
such as record meal, view diet log, et cetera.

12
00:00:33,009 --> 00:00:37,004
The sequence diagram for this flow will look like this.

13
00:00:37,004 --> 00:00:40,004
User chooses login option from Index,

14
00:00:40,004 --> 00:00:43,003
which forwards the request to LoginController,

15
00:00:43,003 --> 00:00:48,001
which in turn forwards it to LoginInput JSP.

16
00:00:48,001 --> 00:00:51,008
LoginInput JSP takes username and password from user

17
00:00:51,008 --> 00:00:54,005
and forwards them to LoginValidator,

18
00:00:54,005 --> 00:00:58,007
which then reaches out to Dao to connect with Red30 DB,

19
00:00:58,007 --> 00:01:01,001
that has user data for validation.

20
00:01:01,001 --> 00:01:04,003
If Dao finds that user credentials match

21
00:01:04,003 --> 00:01:07,006
with the ones in database, it creates User object,

22
00:01:07,006 --> 00:01:10,005
which will now be used for other use cases.

23
00:01:10,005 --> 00:01:13,006
It sends the validation status to LoginResponse,

24
00:01:13,006 --> 00:01:16,000
which displays a message about whether login

25
00:01:16,000 --> 00:01:17,007
was successful or not.

26
00:01:17,007 --> 00:01:20,008
As you can see, the entire flow is in a loop

27
00:01:20,008 --> 00:01:24,003
indicating that system will ask user to login

28
00:01:24,003 --> 00:01:25,009
until successful.

29
00:01:25,009 --> 00:01:27,004
When we think about login,

30
00:01:27,004 --> 00:01:30,005
we also need to think about the flow for logout.

31
00:01:30,005 --> 00:01:33,008
In larger applications, there may be several activities,

32
00:01:33,008 --> 00:01:36,002
such as logging the date and time

33
00:01:36,002 --> 00:01:38,007
when the user signed in or signed out,

34
00:01:38,007 --> 00:01:41,003
closing user-specific resources,

35
00:01:41,003 --> 00:01:44,002
and updating other user-related data.

36
00:01:44,002 --> 00:01:46,006
In this case, all we are doing

37
00:01:46,006 --> 00:01:50,004
when the user chooses logout option from Index JSP,

38
00:01:50,004 --> 00:01:54,001
the control goes to a servlet named Logout,

39
00:01:54,001 --> 00:01:57,002
which gets the session object from request object,

40
00:01:57,002 --> 00:01:59,006
and then invalidates the session.

41
00:01:59,006 --> 00:02:02,007
Putting all the objects identified above gives

42
00:02:02,007 --> 00:02:05,003
us this class diagram.

43
00:02:05,003 --> 00:02:08,000
I have created another package, login,

44
00:02:08,000 --> 00:02:11,000
inside com.hplussport.red30 package

45
00:02:11,000 --> 00:02:12,006
that has three servlets,

46
00:02:12,006 --> 00:02:16,007
LoginController, LoginValidator, and LogOut.

47
00:02:16,007 --> 00:02:18,007
There is a new method in Dao

48
00:02:18,007 --> 00:02:21,005
to validate the user's name and password,

49
00:02:21,005 --> 00:02:23,008
and if the credentials are correct,

50
00:02:23,008 --> 00:02:28,002
then Dao creates a new user instance, which is a bean.

51
00:02:28,002 --> 00:02:30,001
From data model point of view,

52
00:02:30,001 --> 00:02:33,003
we need to add one table to our Red30 DB,

53
00:02:33,003 --> 00:02:35,001
which is the user table.

54
00:02:35,001 --> 00:02:38,000
We have just two fields in the table as of now,

55
00:02:38,000 --> 00:02:40,004
username and password.

56
00:02:40,004 --> 00:02:42,001
In more complex scenarios,

57
00:02:42,001 --> 00:02:44,001
you would need many more parameters,

58
00:02:44,001 --> 00:02:47,001
such as role related to access privileges,

59
00:02:47,001 --> 00:02:51,004
user preferences, record of logins and logouts, and so on.

60
00:02:51,004 --> 00:02:53,004
Let us look at the code now.

61
00:02:53,004 --> 00:02:57,004
Earlier, our header JSP had just one set of links

62
00:02:57,004 --> 00:03:00,001
for non-registered users.

63
00:03:00,001 --> 00:03:03,009
But once the user logs in, then the header has to change

64
00:03:03,009 --> 00:03:06,008
to display links for new use cases.

65
00:03:06,008 --> 00:03:09,006
I have enabled this switch in header JSP

66
00:03:09,006 --> 00:03:12,003
by splitting the JSP into two parts.

67
00:03:12,003 --> 00:03:16,005
In line five, I check if session attribute user is null.

68
00:03:16,005 --> 00:03:19,005
If it is, then I display the old set of links

69
00:03:19,005 --> 00:03:22,005
from line 19 to 22.

70
00:03:22,005 --> 00:03:26,002
If it is not null, then I display another set of links

71
00:03:26,002 --> 00:03:29,005
in lines from 42 to 48.

72
00:03:29,005 --> 00:03:33,005
So, once the user logs in, the reference in line 22

73
00:03:33,005 --> 00:03:38,006
takes us to LoginController, which is inside login package.

74
00:03:38,006 --> 00:03:44,000
LoginController forwards the request to login input JSP.

75
00:03:44,000 --> 00:03:48,008
This has a form in line 13 that has two input boxes

76
00:03:48,008 --> 00:03:51,008
in line 17 and line 21.

77
00:03:51,008 --> 00:03:54,003
These are for username and password.

78
00:03:54,003 --> 00:03:56,001
Once the form is submitted,

79
00:03:56,001 --> 00:03:58,002
the action takes us to validate,

80
00:03:58,002 --> 00:04:01,005
which is the LoginValidator class.

81
00:04:01,005 --> 00:04:05,001
This is where the Dao comes in at line 30,

82
00:04:05,001 --> 00:04:07,005
validates the username and password,

83
00:04:07,005 --> 00:04:10,002
and creates user instance that gets saved

84
00:04:10,002 --> 00:04:13,004
as session variable in line 31.

85
00:04:13,004 --> 00:04:17,008
Finally, the request is forwarded to login response JSP,

86
00:04:17,008 --> 00:04:22,005
which if login was unsuccessful and user object is null,

87
00:04:22,005 --> 00:04:25,002
displays the message in line 17,

88
00:04:25,002 --> 00:04:27,005
or else if it was successful,

89
00:04:27,005 --> 00:04:30,005
the welcome message in line 23.

90
00:04:30,005 --> 00:04:33,009
I have already created a table in Red30 DB,

91
00:04:33,009 --> 00:04:37,005
which we can look at through MySQL Workbench.

92
00:04:37,005 --> 00:04:40,006
As you can see here, I have added two records

93
00:04:40,006 --> 00:04:42,009
with username and password.

94
00:04:42,009 --> 00:04:45,000
Now, let us run our code.

95
00:04:45,000 --> 00:04:47,006
For that, I go back to Eclipse,

96
00:04:47,006 --> 00:04:52,005
and, as usual, Run As, Run on Server,

97
00:04:52,005 --> 00:04:55,009
and wait for initial data load.

98
00:04:55,009 --> 00:04:59,006
Now the application is up, so I go into login

99
00:04:59,006 --> 00:05:03,001
and enter the username and password based on the data

100
00:05:03,001 --> 00:05:07,008
that you just saw, and the login is successful.

101
00:05:07,008 --> 00:05:12,001
You can see new links that are meant for registered users.

102
00:05:12,001 --> 00:05:15,001
If I log out, then I come back to the page

103
00:05:15,001 --> 00:05:17,005
for unregistered users.

104
00:05:17,005 --> 00:05:20,005
At this point, we don't want to publish these changes

105
00:05:20,005 --> 00:05:23,008
to our production server because our real use cases

106
00:05:23,008 --> 00:05:28,000
for recording meals and viewing diet logs are not ready yet.

