Difference between revisions of "Java-Business-Application/C2/Database-and-validation/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border = 1 | Time | Narration |- | 00:01 | Welcome to the spoken-tutorial on''' Database and validation.''' |- | 00:06 | In this tutorial we will learn to: |- | 00:08…')
 
 
(30 intermediate revisions by 5 users not shown)
Line 4: Line 4:
  
 
|-
 
|-
| 00:01
+
| 00:01
| Welcome to the spoken-tutorial on''' Database and validation.'''
+
| Welcome to the '''spoken-tutorial''' on''' Database and validation.'''
  
 
|-
 
|-
 
| 00:06
 
| 00:06
| In this tutorial we will learn to:
+
| In this tutorial, we will learn to:
  
 
|-
 
|-
 
| 00:08
 
| 00:08
| Interact with database
+
|Interact with database and 
  
 
|-
 
|-
| 00:10
+
|00:10
| Validate the fields  
+
|'''Validate''' the '''fields'''.
 
+
  
 
|-
 
|-
 
| 00:12
 
| 00:12
|Here we are using
+
| Here we are using:'''Ubuntu''' Version 12.04
  
 
|-
 
|-
| 00:13
+
|00:15
| Ubuntu Version 12.04
+
|'''Netbeans IDE''' 7.3
  
 
|-
 
|-
| 00:15
+
|00:19
| Netbeans IDE 7.3
+
|'''JDK''' 1.7
  
 
|-
 
|-
| 00:19
+
|00:21
| JDK 1.7
+
|'''Firefox''' web-browser 21.0
 
+
|-
+
| 00:21
+
| Firefox web-browser 21.0
+
  
 
|-
 
|-
Line 46: Line 41:
 
|-
 
|-
 
| 00:28
 
| 00:28
|To follow this tutorial you must have knowledge of
+
|To follow this tutorial, you must have knowledge of-
  
 
|-
 
|-
 
| 00:31
 
| 00:31
| Basics of '''Java Servlets '''and '''JSPs'''
+
|Basics of '''Java Servlets''' and '''JSPs'''
 +
 
 
|-
 
|-
| 00:35
+
|00:35
| Connecting to '''MySQL Database '''  from '''Netbeans IDE'''
+
|Connecting to '''MySQL Database '''  from '''Netbeans IDE'''
  
 
|-
 
|-
 
| 00:39
 
| 00:39
| Creating '''database '''and '''tables'''
+
|Creating '''database '''and '''tables'''
  
 
|-
 
|-
Line 64: Line 60:
  
 
|-
 
|-
| 00:47
+
|00:47
 
|Now, let us go to '''Netbeans IDE.'''
 
|Now, let us go to '''Netbeans IDE.'''
  
 
|-
 
|-
 
| 00:52
 
| 00:52
| I have started the '''MySQL server.'''
+
| I have started the '''MySQL server.'''
  
 
|-
 
|-
| 00:55
+
| 00:55
| I have created a '''database ''' in it named  '''library.'''
+
| I have created a '''database''' in it named  '''library.'''
  
 
|-
 
|-
 
| 01:00
 
| 01:00
| I have created a '''table '''in it named '''Users.'''
+
| I have created a '''table '''in it named '''Users.'''
  
 
|-
 
|-
| 01:04
+
| 01:04
| I have already inserted some values into this table.
+
|I have already inserted some values into this '''table'''.
  
 
|-
 
|-
| 01:08
+
| 01:08
| I will show them now.
+
| I will show them now.
  
 
|-
 
|-
 
| 01:10
 
| 01:10
| For that, right click on '''Users '''and click on '''View Data.'''
+
| For that, right click on '''Users '''and click on '''View Data.'''
  
 
|-
 
|-
 
| 01:15
 
| 01:15
|Click on the '''Output '''button at the bottom.
+
|Click on the '''Output''' button at the bottom.
  
 
|-
 
|-
| 01:19
+
| 01:19
| We can see that there are '''15 users '''here.
+
| We can see that there are '''15 users '''here.
  
 
|-
 
|-
 
| 01:23
 
| 01:23
|   We can see the '''FirstName, Surname, Age, Gender, Email, Username '''and '''Password. '''
+
| We can see the '''FirstName, Surname, Age, Gender, email, Username''' and '''Password. '''
  
 
|-
 
|-
| 01:31
+
| 01:31
| Now, let us load the '''JDBC''' driver that is '''Java Database Connectivity Driver.'''
+
| Now, let us '''load''' the '''JDBC driver''' that is '''Java Database Connectivity Driver.'''
  
 
|-
 
|-
| 01:39
+
| 01:39
 
| For that, click on the '''Projects tab.'''
 
| For that, click on the '''Projects tab.'''
  
 
|-
 
|-
| 01.42
+
| 01:42
| Right click on '''Libraries''' and  Click on '''Add Library.'''
+
| Right-click on '''Libraries''' and  click on '''Add Library.'''
  
 +
|-
 +
| 01:46
 +
| Then click on '''MySQL JDBC Driver.'''
  
 
|-
 
|-
| 01.46
+
| 01:50
| Then click on '''MySQL JDBC Driver.'''
+
| And click on '''Add Library.'''
  
 
|-
 
|-
| 01.50
+
| 01:53
| And click on '''Add Library.'''
+
| This will load the '''JDBC Driver.'''
  
 
|-
 
|-
| 01.53
+
| 01:56
| This will load the '''JDBC Driver.'''
+
| Let us '''run''' the '''Project'''as we had done earlier.
  
 
|-
 
|-
| 01.56
+
| 02:00
Let us '''run '''the '''Project '''as we had done earlier.
+
| Now, type the '''User Name '''as '''"arya" '''and '''Password '''as '''"arya123*".'''
  
 
|-
 
|-
| 02.00
+
| 02:06
| Now,  type the '''username '''as '''arya '''and '''password '''as '''arya123*.'''
+
| Then click on '''Sign In.'''
  
 
|-
 
|-
| 02.06
+
| 02:08
| Then click on '''Sign In.'''
+
| We can see the '''Success Greeting Page'''.
  
 
|-
 
|-
| 02.08
+
| 02:12
| We can see the '''successGreeting '''page.
+
| Click on '''here''' to '''logout'''.
  
 
|-
 
|-
| 02.12
+
| 02:15
| Click on '''here '''to logout.
+
| Now, let us switch back to the '''IDE.'''
  
 
|-
 
|-
| 02.15
+
| 02:17
| Now, let us switch back to the '''IDE.'''
+
| We will go to '''GreetingServlet '''dot '''java.'''
  
 
|-
 
|-
|  02.17
+
|  02:21
| We will go to '''GreetingServlet '''''dot '''''java.'''
+
| Come to the '''doPost''' '''method'''.
|-
+
|  02.21
+
| Come to the '''doPost''' method
+
  
 
|-
 
|-
| 02.23
+
| 02:23
| First, we get the '''Username '''and '''Password '''from the '''request '''using '''getParameter method.'''
+
| First, we get the '''username '''and '''password '''from the '''request '''using '''getParameter() method.'''
  
 
|-
 
|-
| 02.31
+
| 02:31
| Next we will see the code for '''JDBC connection.'''
+
| Next, we will see the code for '''JDBC connection.'''
  
 
|-
 
|-
| 02.35
+
| 02:35
| We have initialized the '''Connection object, PreparedStatement object '''and '''Resultset object '''to '''null.'''
+
| First, we have initialized the '''Connection object, PreparedStatement object '''and '''ResultSet object '''to '''null.'''
  
 
|-
 
|-
| 02.44
+
| 02:44
 
|  Then we '''register '''the '''driver '''in our '''program.'''
 
|  Then we '''register '''the '''driver '''in our '''program.'''
  
 
|-
 
|-
| 02.48
+
| 02:48
| Then we create a connection to the '''database.'''
+
| Then we create a connection to the '''database.'''
  
 
|-
 
|-
| 02.52
+
| 02:52
| Then, we execute the '''prepareStatement method '''on the '''Connection object.'''
+
| Then, we '''execute''' the '''prepareStatement method '''on the '''Connection object.'''
  
 
|-
 
|-
| 02.58
+
| 02:58
| We give the query to get the '''user details '''from '''Users table.'''
+
| We give the '''query''' to get the user details from '''Users''' table.
  
 
|-
 
|-
| 03.03
+
| 03:03
| We check if '''username '''and '''password '''is same as that which is entered in the form.
+
| We check if '''username '''and '''password '''are same as that which are entered in the '''form'''.
  
 
|-
 
|-
| 03.09
+
| 03:09
| Here, the question mark denotes each field in the database.
+
| Here, the question mark denotes each '''field''' in the database.
  
 
|-
 
|-
| 03.15
+
| 03:15
| To supply values in place of question mark, we execute the '''setString method.'''
+
| To supply values in place of question mark, we execute the '''setString()''' '''method.'''
  
 
|-
 
|-
| 03.22
+
| 03:22
 
| We do this using the '''PreparedStatement object.'''
 
| We do this using the '''PreparedStatement object.'''
  
 
|-
 
|-
| 03.26
+
| 03:26
| Then we execute the '''executeQuery method '''on the '''Prepared statement''' object
+
| Then we execute the '''executeQuery method '''on the '''Prepared statement''' object.
 +
 
 
|-
 
|-
|03.33
+
|03:33
|We store  the result in '''ResultSet''' object
+
|We store  the result in '''ResultSet''' object.
  
 
|-
 
|-
|03.37
+
|03:37
| For successful login, we display the '''successGreeting page.'''
+
|For successful login, we display the '''successGreeting page.'''
  
 
|-
 
|-
| 03.43
+
| 03:43
| For this, we use the '''RequestDispatcher interface.'''
+
| For this, we use the '''RequestDispatcher interface.'''
  
 
|-
 
|-
| 03.48
+
| 03:48
| We use the '''getRequestDispatcher method '''on the '''request '''to obtain the '''RequestDispatcher object.'''
+
| We use the '''getRequestDispatcher()''' method on the '''request '''to obtain the '''RequestDispatcher''' object.
  
 
|-
 
|-
| 03.56
+
| 03:56
|  We then invoke the '''forward method '''on '''RequestDispatcher.''' object
+
|  We then invoke the '''forward()''' method on '''RequestDispatcher.''' object.
  
 
|-
 
|-
| 04.02
+
|04:02
| In this way, we '''forward '''to '''successGreeting '''''dot '''''jsp.'''
+
|In this way, we '''forward '''to '''successGreeting '''dot '''jsp.'''
  
 
|-
 
|-
| 04.07
+
|04:07
|Now switch back to the slides
+
|Now switch back to the '''slides'''.
  
 
|-
 
|-
| 04.10
+
| 04:10
|Let us learn something about '''RequestDispatcher interface'''
+
|Let us learn something about '''RequestDispatcher interface'''.
  
 
|-
 
|-
| 04.15
+
| 04:15
| This  interface provides the facility of dispatching  the '''request''' to another resouce
+
| This  '''interface''' provides the facility of dispatching  the '''request''' to another resource.
  
 
|-
 
|-
| 04.22
+
| 04:22
| The resorce can be html, servlet, or jsp
+
| The resource can be '''html, servlet''' or '''jsp'''.
  
 
|-
 
|-
| 04.26
+
| 04:26
| Now let us come back to the IDE
+
| Now, let us come back to the '''IDE'''.
 +
 
 
|-
 
|-
|  04.29
+
|  04:29
|  Let us come to '''successGreeting '''''dot '''''jsp.'''
+
|  Let us come to '''successGreeting ''' '''dot''''''jsp.'''
  
 
|-
 
|-
|  04.33
+
|  04:33
 
|  Here, we are displaying the success message '''You have successfully logged in.'''
 
|  Here, we are displaying the success message '''You have successfully logged in.'''
 
 
  
 
|-
 
|-
Line 264: Line 260:
  
 
|-
 
|-
|  04.41
+
|  04:41
 
| Type a '''username '''and '''password '''that we have not included in the '''database.'''
 
| Type a '''username '''and '''password '''that we have not included in the '''database.'''
  
 
|-
 
|-
|  04.47
+
|  04:47
 
|  So, let me type '''abc '''as the '''username '''and '''abc123* '''as the '''password.'''
 
|  So, let me type '''abc '''as the '''username '''and '''abc123* '''as the '''password.'''
  
 
|-
 
|-
| 04.56
+
| 04:56
 
|  Then click on '''Sign In.'''
 
|  Then click on '''Sign In.'''
  
 
|-
 
|-
| 04.59
+
| 04:59
|  We can see that we get the '''error message '''on the same page itself.
+
|  We can see that we get the '''error message '''on the same '''page''' itself.
  
 
|-
 
|-
| 05.03
+
| 05:03
| '''Please correct the following error!!! Invalid username or password'''
+
| ''' "Please correct the following errors!!! Invalid username or password" '''.
  
 
|-
 
|-
|  05.09
+
|  05:09
 
|  Now, let us see the code for this.  
 
|  Now, let us see the code for this.  
  
 
|-
 
|-
| 05.12
+
| 05:12
| So, Switch back to the '''IDE'''
+
| So, switch back to the '''IDE'''.
 
|-
 
|-
| 05.14
+
| 05:14
|  Go to '''GreetingServlet '''''dot '''''java.'''
+
|  Go to '''GreetingServlet '''dot '''java.'''
  
 
|-
 
|-
|  05.17
+
|  05:17
|  If the '''validation '''fails, then we should display the '''error messages.'''
+
|  If the '''validation '''fails then we should display the '''error message.'''
  
 
|-
 
|-
| 05.22
+
| 05:22
| First we have  initialized a '''List '''of '''errorMsgs.'''
+
| First, we have  initialized a '''List '''of '''errorMsgs.'''
  
 
|-
 
|-
|  05.27
+
|  05:27
|  We set the variable '''errorMsgs '''into the '''request scope''' using '''setAttribute '''method'''.'''
+
|  We set the variable '''errorMsgs '''into the '''request scope''' using '''setAttribute''' '''method'''.
  
 
|-
 
|-
|05.35
+
|05:35
|  Here, '''errorMsgs '''is the '''attribute name.'''
+
|  Here, '''errorMsgs '''is the '''attribute''' name.
  
 
|-
 
|-
| 05.39
+
| 05:39
 
|  We have initialized a '''String variable id '''to '''null.'''
 
|  We have initialized a '''String variable id '''to '''null.'''
  
 
|-
 
|-
|  05.44
+
|  05:44
 
|  Then, we check if the '''user '''exists in the '''database.'''
 
|  Then, we check if the '''user '''exists in the '''database.'''
  
 
|-
 
|-
| 05.48
+
| 05:48
 
| If yes, we store the value in the variable '''id.'''
 
| If yes, we store the value in the variable '''id.'''
  
 
|-
 
|-
|  05.53
+
|  05:53
|  Else, we add the error '''Invalid username or password '''to '''errorMsgs list.'''
+
|  Else, we add the error '''"Invalid username or password" ''' to '''errorMsgs''' list.
  
 
|-
 
|-
|  06.00
+
|  06:00
|  If the '''errorMsgs list '''is not empty, we display the '''error messages '''on '''index '''''dot '''''jsp.'''
+
|  If the '''errorMsgs list '''is not empty then we display the '''error messages '''on '''index '''dot '''jsp.'''
 
+
  
 
|-
 
|-
| 06.09
+
| 06:09
| So, we have to redirect to '''index '''''dot '''''jsp.'''
+
| So, we have to redirect to '''index ''' dot '''jsp.'''
 
+
  
 
|-
 
|-
| 06.13
+
| 06:13
| We have already seen how to redirect to another page using '''RequestDispatcher.'''
+
| We have already seen how to redirect to another '''page''' using '''RequestDispatcher.'''
  
 
|-
 
|-
|  06.20
+
|  06:20
 
|  Note that we have included this code inside the '''try catch block '''to handle '''exception '''scenarios.
 
|  Note that we have included this code inside the '''try catch block '''to handle '''exception '''scenarios.
  
 
|-
 
|-
|  06.27
+
|  06:27
|  Now, we will see how to fetch '''errorMsgs '''variable in '''index '''''dot '''''jsp.'''
+
|  Now, we will see how to fetch '''errorMsgs '''variable in '''index '''dot '''jsp.'''
  
 
|-
 
|-
|  06.34
+
|  06:34
|  First, we obtain the value of the '''attribute errorMsgs.'''
+
|  First, we obtain the value of the '''attribute "errorMsgs".'''
  
 
|-
 
|-
| 06.38
+
| 06:38
 
| This is done using  '''getAttribute method '''on the '''request.'''
 
| This is done using  '''getAttribute method '''on the '''request.'''
  
 
|-
 
|-
| 06.44
+
| 06:44
| Note that we have included the '''Java code '''within the opening tag  which is ''less than sign percentage sign ''and the ''closing tag percentage sign and greater than sign.''
+
| Note that we have included the '''Java code '''within the opening tag  which is ''less than sign percentage sign ''and the closing tag ''percentage sign and greater than sign.''
  
 
|-
 
|-
| 06.57
+
| 06:57
| This block of code is known as '''scriptlet.'''
+
| This block of code is known as '''scriptlet.'''
  
 
|-
 
|-
| 07.02
+
| 07:02
 
| It contains '''Java code '''which is executed every time '''JSP '''is invoked.
 
| It contains '''Java code '''which is executed every time '''JSP '''is invoked.
  
 
|-
 
|-
|  07.08
+
|  07:08
|  If the value of '''errorMsgs '''is not null, then we  display this message.
+
|  If the value of '''errorMsgs '''is not null then we  display this message.
  
 
|-
 
|-
| 07.15
+
| 07:15
| '''Please correct the following errors.'''
+
| '''"Please correct the following errors".'''
  
 
|-
 
|-
 
| 07:18
 
| 07:18
|  Then we iterate through list of  '''errorMsgs.'''
+
|  Then we iterate through the list of  '''errorMsgs.'''
  
 
|-
 
|-
|  07.23
+
|  07:23
 
|  We then display the '''error messages '''as a '''list.'''
 
|  We then display the '''error messages '''as a '''list.'''
  
 
|-
 
|-
|  07.27
+
|  07:27
|  This is how we display the '''error messages '''on  '''index '''''dot '''''jsp.'''
+
|  This is how we display the '''error messages''' on  '''index dot jsp.'''
  
 
|-
 
|-
|  07.32
+
|  07:32
 
|  Now, let us see how to add a''' user''' into the '''database'''.
 
|  Now, let us see how to add a''' user''' into the '''database'''.
  
 
|-
 
|-
|  07.37
+
|  07:37
 
|  Before adding a '''user '''into the '''database, '''we have to create a '''model '''for the '''User table.'''
 
|  Before adding a '''user '''into the '''database, '''we have to create a '''model '''for the '''User table.'''
  
 
|-
 
|-
| 07.44
+
| 07:44
| Now, Let us see what a '''model '''is.
+
| Now, let us see what a '''model '''is.
  
 
|-
 
|-
|07.48
+
|07:48
| A '''model :'''
+
| A '''model :'''Represents the underlying '''logical structure''' of data in a '''software application.'''
  
 
|-
 
|-
| 07.49
+
| 07:55
| Represents the underlying, logical structure of data in a '''software application.'''
+
|A '''Java class '''with '''attributes '''and '''setters '''and '''getters '''for them.
  
 
|-
 
|-
| 07.55
+
| 08:00
|  A '''Java class '''with '''attributes '''and '''setters '''and '''getters '''for them.
+
 
+
|-
+
| 08.00
+
 
|  In this way, we can consider '''model '''as a whole instead of the individual '''attributes.'''
 
|  In this way, we can consider '''model '''as a whole instead of the individual '''attributes.'''
  
 
|-
 
|-
|  08.07
+
|  08:07
|  Now, Switch back to '''Netbeans IDE.'''
+
|  Now, switch back to '''Netbeans IDE.'''
  
 
|-
 
|-
| 08.11
+
| 08:11
| I have already created the model '''User '''''dot '''''java.'''
+
| I have already created the model '''User '''dot '''java.'''
  
 
|-
 
|-
| 08.16
+
| 08:16
|  Note that we have created this '''Java class '''inside the '''package org ''dot ''spokentutorial ''dot ''model.'''
+
|  Note that we have created this '''Java class '''inside the '''package org dot spokentutorial dot model.'''
  
 
|-
 
|-
|08.24
+
|08:24
|  We have the following '''attributes firstName, surname, age, gender, email, username, password.'''
+
|  We have the following attributes- '''firstName, surname, age, gender, email, username''' and '''password'''.
 
+
  
 
|-
 
|-
| 08.33
+
| 08:33
 
| We have initialized them to empty values.
 
| We have initialized them to empty values.
  
 
|-
 
|-
|  08.37
+
|  08:37
 
|  Then we have a '''parameterized constructor.'''
 
|  Then we have a '''parameterized constructor.'''
  
 
|-
 
|-
|  08.41
+
|  08:41
 
|  We also have the '''default constructor.'''
 
|  We also have the '''default constructor.'''
  
 
|-
 
|-
|  08.44
+
|  08:44
 
|  We define the '''getFirstName method.'''
 
|  We define the '''getFirstName method.'''
  
 
|-
 
|-
| 08.47
+
| 08:47
 
|  We also define the '''setFirstName method.'''
 
|  We also define the '''setFirstName method.'''
  
 
|-
 
|-
|  08.51
+
|  08:51
Similary we define the '''set '''and '''get methods '''on each of the '''attributes.'''
+
Similarly, we define the '''set '''and '''get methods '''on each of the '''attributes.'''
  
 
|-
 
|-
| 08.57
+
| 08:57
 
|  Come back to the '''browser'''.
 
|  Come back to the '''browser'''.
  
 
|-
 
|-
| 08.59
+
| 08:59
|  Now, let us click on '''here link '''to '''register.'''
+
|  Now, let us click on '''here''' link to '''register.'''
  
 
|-
 
|-
|  09.03
+
|  09:03
|  Type all the fields in the '''Registration Page.'''
+
'''Type''' all the '''fields''' in the '''Registration page.'''
  
 
|-
 
|-
|  09.07
+
|  09:07
 
|  Then click on '''Add User.'''
 
|  Then click on '''Add User.'''
  
 
|-
 
|-
|  09.10
+
|  09:10
|  We get  '''Add User Success Page.'''
+
|  We get  '''Add User Success''' '''page'''.
  
 
|-
 
|-
|  09.14
+
|  09:14
|  We get  the message ''' Your request to add harshita was successful.'''
+
|  We get  the message: ''' "Your request to add harshita was successful".'''
  
 
|-
 
|-
|  09.20
+
|  09:20
|  Here '''harshita''' was the '''username''' that we have given  
+
|  Here '''harshita''' was the '''username''' that we have given.
  
 
|-
 
|-
|  09.24
+
|  09:24
 
|  Now, let us see how this is done.
 
|  Now, let us see how this is done.
  
 
|-
 
|-
|  09.28
+
|  09:28
|  So switch back to the '''IDE.'''
+
|  So, switch back to the '''IDE.'''
  
 
|-
 
|-
|  09.30
+
|  09:30
|  Go to  '''AddUserServlet '''''dot '''''java.'''
+
|  Go to  '''AddUserServlet''' '''dot ''' '''java.'''
  
 
|-
 
|-
|  09.35
+
|  09:35
|  The steps are similar to that we had followed in '''GreetingServlet '''''dot '''''java.'''
+
|  The steps are similar to that we had followed in '''GreetingServlet '''dot '''java.'''
  
 
|-
 
|-
|  09.40
+
|  09:40
|  First we get the '''form parameters '''using the '''getParameter method.'''
+
|  First, we get the '''form parameters''' using the '''getParameter method.'''
  
 
|-
 
|-
| 09.46
+
| 09:46
|  We initialize variable '''user '''as '''instance '''of '''User Model '''with individual '''attributes.'''
+
|  We initialize variable '''user '''as '''instance '''of '''User model '''with individual '''attributes.'''
  
 
|-
 
|-
|  09.53
+
|  09:53
 
|  We set the variable '''user '''into the '''request scope '''using '''setAttribute method.'''
 
|  We set the variable '''user '''into the '''request scope '''using '''setAttribute method.'''
  
 
|-
 
|-
| 10.01
+
| 10:01
|  If there are no errors while filling  the '''form, '''we execute the query to  insert the values into the user tables.
+
|  If there are no errors while filling  the '''form, '''we '''execute''' the '''query''' to  insert the values into the '''Users''' table.
  
 
|-
 
|-
|  10.10
+
|  10:10
|  Then we forward to '''success user page'''
+
|  Then we forward to '''successUser''' page.
 
+
  
 
|-
 
|-
|  10.15
+
|  10:15
|  Now, let us come to '''successUser '''''dot '''''jsp.'''
+
|  Now, let us come to '''successUser ''' ''dot '' '''jsp.'''
  
 
|-
 
|-
|10.19   
+
|10:19   
|  First, we have imported '''User '''''dot '''''java.'''
+
|  First, we have imported '''User '''dot '''java.'''
  
 
|-
 
|-
| 10.24
+
| 10:24
 
| This line of code is called '''directive '''in '''JSP.'''
 
| This line of code is called '''directive '''in '''JSP.'''
  
 
|-
 
|-
| 10.28
+
| 10:28
|  A '''JSP directive '''starts with ''opening tag less than sign percentage sign and at the rate sign ''and ends with ''closing tag percentage sign and greater than sign.''
+
|  A '''JSP directive''' starts with opening tag- 'less than sign percentage sign and at the rate sign' and ends with closing tag- 'percentage sign and greater than sign'.
  
 
|-
 
|-
|  10.42
+
|  10:42
 
|  This one is a '''page directive.'''
 
|  This one is a '''page directive.'''
 
  
 
|-
 
|-
| 10.45
+
| 10:45
 
| The '''page directive '''contains a list of all imported '''packages.'''
 
| The '''page directive '''contains a list of all imported '''packages.'''
  
 
|-
 
|-
|  10.50
+
|  10:50
 
|  We get the value of the attribute '''user '''and store it as the '''User object.'''
 
|  We get the value of the attribute '''user '''and store it as the '''User object.'''
  
 
|-
 
|-
|  10.57
+
|  10:57
|  Then,We have the '''success message '''here.
+
|  Then, we have the '''success message '''here.
  
 
|-
 
|-
|  11.00
+
|  11:00
 
|  Here, we have retrieved the '''Username.'''
 
|  Here, we have retrieved the '''Username.'''
  
 
|-
 
|-
| 11.04
+
| 11:04
| We have''' '''used the '''getUsername() method '''on the '''request object.'''
+
| We have used the '''getUsername() method '''on the '''request object.'''
  
 
|-
 
|-
|  11.09
+
|  11:09
 
|  We have done this using '''scriptlet tags.'''
 
|  We have done this using '''scriptlet tags.'''
  
 
|-
 
|-
|  11.12
+
|  11:12
 
|  Now, let us come back to the browser.
 
|  Now, let us come back to the browser.
  
 
|-
 
|-
|  11.15
+
|  11:15
 
|  We will try to add a '''user '''already present in the database.
 
|  We will try to add a '''user '''already present in the database.
  
 
|-
 
|-
|  11.20
+
|  11:20
|  So, I will add '''harshita '''again.
+
|  So, I will try to add '''harshita '''again.
  
 
|-
 
|-
| 11.24
+
| 11:24
|  We can see that we get the error message '''Please correct the following errors!!1 Duplicate entry 'harshita' for key username.'''
+
|  We can see that we get the error message ''' "Please correct the following errors!!!! Duplicate entry 'harshita' for key 'UserName'.'''
  
 
|-
 
|-
|  11.33
+
|  11:33
| Now, Let us register for a user once again.
+
| Now, let us '''register''' for a user once again.
  
 
|-
 
|-
|  11.37
+
|  11:37
|  Here, I have filled up the form now.
+
|  Here, I have filled up the '''form''' now.
  
 
|-
 
|-
|  11.40
+
|  11:40
|  I have created a mistake in the '''Age''' field.
+
|  I have created a mistake in the '''Age''' '''field'''.
  
 
|-
 
|-
|  11.44
+
|  11:44
 
|  I have typed '''ab '''instead of a valid number.
 
|  I have typed '''ab '''instead of a valid number.
  
 
|-
 
|-
|  11.48
+
|  11:48
 
|  Now, click on '''Add User.'''
 
|  Now, click on '''Add User.'''
  
 
|-
 
|-
|  11.51
+
|  11:51
|  We see we get the error message '''The age must be a positive integer.'''
+
|  We see, we get the '''error''' message- '''"The age must be a positive integer".'''
  
 
|-
 
|-
|  11.57
+
|  11:57
 
|  Now, let us see how this is done.
 
|  Now, let us see how this is done.
 
  
 
|-
 
|-
| 12.00
+
| 12:00
 
| Switch back to the '''IDE.'''
 
| Switch back to the '''IDE.'''
  
 
|-
 
|-
|  12.03
+
|  12:03
|  Open '''AddUserServlet '''''dot '''''java.'''
+
|  Open '''AddUserServlet '''dot '''java.'''
  
 
|-
 
|-
|  12.08
+
|  12:08
|  Here also, we have created a '''list for errorMsgs.'''
+
|  Here also, we have created a list for '''errorMsgs.'''
  
 
|-
 
|-
|  12.11
+
|  12:11
 
|  Then we set the variable '''errorMsgs '''into the '''request scope '''using '''setAttribute method.'''
 
|  Then we set the variable '''errorMsgs '''into the '''request scope '''using '''setAttribute method.'''
  
 
|-
 
|-
|  12.18
+
|  12:18
|  Then, we have declared '''ageUser '''of type '''integer''' and we have initialize it to -1
+
|  Then, we have declared '''ageUser '''of type '''integer''' and we have initialized it to -1 (minus one).
  
 
|-
 
|-
|  12.26
+
|  12:26
|  Inside the '''try catch block '''we have used '''parseInt method.'''
+
|  Inside the '''try catch block, '''we have used '''parseInt method.'''
 
+
  
 
|-
 
|-
| 12.31
+
| 12:31
| This will return an integer, given a string representation of number as input.
+
| This will return an integer, given a '''string''' representation of number as input.
  
 
|-
 
|-
| 12.37
+
| 12:37
|  So here we validate that the '''age field''' contains a valid positive integer.
+
|  So, here we validate that the '''age field''' contains a valid positive integer.
 
+
  
 
|-
 
|-
| 12.44
+
| 12:44
| If the validation fails, then we add error message to '''errorMsgs list'''
+
| If the validation fails then we add error message to '''errorMsgs list'''.
  
 
|-
 
|-
|  12.51
+
|  12:51
|  The age must be a positive integer.
+
|  The '''age''' must be a positive integer.
 
+
  
 
|-
 
|-
|  12.54
+
|  12:54
|  Similarly, we have to validate that all the other fields too have valid data.
+
|  Similarly, we have to validate all the other '''fields''' to have valid data.
  
 
|-
 
|-
| 13.01
+
| 13:01
|  If the errorMsgs list is not empty, then we will display the '''errorMsgs '''on '''addUser '''''dot '''''jsp''' itself
+
|  If the '''errorMsgs''' list is not empty then we will display the '''errorMsgs '''on '''addUser '''dot '''jsp''' itself.
  
 
|-
 
|-
| 13.09
+
| 13:09
 
| We have already seen how to do this using the '''RequestDispatcher.'''
 
| We have already seen how to do this using the '''RequestDispatcher.'''
  
 
|-
 
|-
|  13.15
+
|  13:15
|  Now, let us come to the '''addUser '''''dot '''''jsp.'''
+
|  Now, let us come to '''addUser '''dot '''jsp.'''
  
 
|-
 
|-
|  13.19
+
|  13:19
|  Here also, first we have imported '''User '''''dot '''''java.'''
+
|  Here also, first we have imported '''User '''dot '''java.'''
  
 
|-
 
|-
| 13.24
+
| 13:24
|  Inside the '''scriptlet tags '''we have created an '''object '''of type '''User.'''
+
|  Inside the '''scriptlet tags,''' we have created an '''object '''of type '''User.'''
  
 
|-
 
|-
|  13.31
+
|  13:31
|  Then we get the value of the attribute '''errorMsgs '''using '''getAttribute method.'''
+
|  Then we get the value of the attribute '''errorMsgs''' using '''getAttribute method.'''
  
 
|-
 
|-
|  13.38
+
|  13:38
|  We check if this value is equal to '''null.'''
+
|  We check if this value is equal to null.
  
 
|-
 
|-
| 13.43
+
| 13:43
|  If it is not equal to null, then  we display the error message just as we had done for '''index '''''dot '''''jsp.'''
+
|  If it is not equal to null then  we display the error message just as we had done for '''index ''' '''dot''' '''jsp.'''
  
 
|-
 
|-
| 13.51
+
| 13:51
|  If not, we will get the value of the '''attribute user from request '''using  '''User model.'''
+
|  If not, we will get the value of the attribute '''user''' from '''request '''using  ''''User' model.'''
  
 
|-
 
|-
|  13.59
+
|  13:59
 
|  Then we have the '''form.'''
 
|  Then we have the '''form.'''
  
 
|-
 
|-
|  14.01
+
|  14:01
 
|  The '''form tag '''has '''action '''as '''AddUserServlet '''and '''method '''as '''POST.'''
 
|  The '''form tag '''has '''action '''as '''AddUserServlet '''and '''method '''as '''POST.'''
  
 
|-
 
|-
|  14.07
+
|  14:07
|  The first field is '''First Name '''of '''input type '''as''' text ''','''name '''as '''firstName '''and '''value '''as '''user '''''dot '''''getFirstName.'''
+
|  The first field is '''First Name '''of '''input type '''as''' text ''', '''name '''as '''firstName ''' and '''value '''as '''user '''dot '''getFirstName.'''
 
+
  
 
|-
 
|-
| 14.18
+
| 14:18
 
| Here, we are initializing the value of '''firstName '''to empty string.
 
| Here, we are initializing the value of '''firstName '''to empty string.
  
 
|-
 
|-
|  14.24
+
|  14:24
|  Similarly you have to do for other fields.
+
|  Similarly, you have to do for other '''fields'''.
  
 
|-
 
|-
| 14.28
+
| 14:28
|  We also have a  '''submit ''' button and value as  ''' Add User.'''
+
|  We also have a  '''submit ''' button and '''value''' as  ''' Add User.'''
 
+
  
 
|-
 
|-
|  14.33
+
|  14:33
|  This is how we validate the fields in '''addUser.jsp.'''
+
|  This is how we validate the '''fields''' in '''addUser.jsp.'''
  
 
|-
 
|-
|  14.38
+
|  14:38
|  You can try out different errors on the '''Add User page.'''
+
|  You can try out different '''errors''' on '''"addUser" page.'''
  
 
|-
 
|-
|  14.42
+
|  14:42
|  Now let us see that the user '''harshita''' has been added to the '''database.'''
+
|  Now, let us see if the '''user''' '''harshita''' has been added to the '''database.'''
  
 
|-
 
|-
|  14.49
+
|  14:49
| So come back to user table. We can see that harshita is added to the database.
+
| So, come back to the '''Users''' table. We can see that '''harshita''' is added to the database.
 
+
  
 
|-
 
|-
| 14.56
+
| 14:56
| In this tutorial we have learnt:
+
| In this tutorial, we have learnt:
  
 
|-
 
|-
| 14.58
+
| 14:58
|  Database connectivity and
+
'''Database connectivity''' and
  
 
|-
 
|-
| 15.00
+
| 15:00
|  Field validation
+
'''Field validation'''.
  
 
|-
 
|-
| 15.02
+
| 15:02
 
|  To know more about the spoken tutorial project, watch the video available at the following link.
 
|  To know more about the spoken tutorial project, watch the video available at the following link.
  
 
|-
 
|-
| 15.07
+
| 15:07
|  It summarizes the Spoken Tutorial Project
+
|  It summarizes the Spoken Tutorial Project.
  
 
|-
 
|-
| 15.11
+
| 15:11
|  If you do not have good bandwidth you can download and watch it
+
|  If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
|  15.15
+
|  15:15
|  The Spoken Tutorial Project Team
+
|  The Spoken Tutorial Project team:
  
 
|-
 
|-
| 15.17
+
| 15:17
|  Conducts workshops using spoken tutorials
+
|  Conducts workshops using spoken tutorials.
  
 
|-
 
|-
| 15.20
+
| 15:20
|  Gives certificates for those who pass an online test
+
|  Gives certificates for those who pass an online test.
  
 
|-
 
|-
| 15.23
+
| 15:23
|  For more details please write to contact at spoken hyphen tutorial dot org
+
|  For more details, please write to: contact at spoken hyphen tutorial dot org.
  
 
|-
 
|-
| 15.29
+
| 15:29
|  Spoken Tutorial Project is a part of the Talk to a Teacher Project
+
|  Spoken Tutorial Project is a part of the "Talk to a Teacher Project".
  
 
|-
 
|-
| 15.32
+
| 15:32
|  It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
|  It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
 
|-
 
|-
| 15.38
+
| 15:38
 
|  More information on this mission is available at spoken-tutorial.org/NMEICT-intro
 
|  More information on this mission is available at spoken-tutorial.org/NMEICT-intro
  
 
|-
 
|-
| 15.48
+
| 15:48
|  The Library Management System has been contributed by a leading software MNC, through their Corporate Social Responsibility Programme.
+
|  The '''Library Management System''' has been contributed by a leading software MNC through their "Corporate Social Responsibility Programme".
  
 
|-
 
|-
| 15.57
+
| 15:57
 
| They have also validated the content for this spoken tutorial.
 
| They have also validated the content for this spoken tutorial.
 
  
 
|-
 
|-
| 16.02
+
| 16:02
| This is Arya Ratish from IIT Bombay signing off.
+
| This is Arya Ratish from IIT Bombay, signing off.
 
+
 
+
 
Thank you for joining.
 
Thank you for joining.
  
 
|}
 
|}

Latest revision as of 16:59, 9 March 2017

Time Narration
00:01 Welcome to the spoken-tutorial on Database and validation.
00:06 In this tutorial, we will learn to:
00:08 Interact with database and
00:10 Validate the fields.
00:12 Here we are using:Ubuntu Version 12.04
00:15 Netbeans IDE 7.3
00:19 JDK 1.7
00:21 Firefox web-browser 21.0
00:24 You can use any web-browser of your choice.
00:28 To follow this tutorial, you must have knowledge of-
00:31 Basics of Java Servlets and JSPs
00:35 Connecting to MySQL Database from Netbeans IDE
00:39 Creating database and tables
00:42 If not, for relevant tutorials please visit our website.
00:47 Now, let us go to Netbeans IDE.
00:52 I have started the MySQL server.
00:55 I have created a database in it named library.
01:00 I have created a table in it named Users.
01:04 I have already inserted some values into this table.
01:08 I will show them now.
01:10 For that, right click on Users and click on View Data.
01:15 Click on the Output button at the bottom.
01:19 We can see that there are 15 users here.
01:23 We can see the FirstName, Surname, Age, Gender, email, Username and Password.
01:31 Now, let us load the JDBC driver that is Java Database Connectivity Driver.
01:39 For that, click on the Projects tab.
01:42 Right-click on Libraries and click on Add Library.
01:46 Then click on MySQL JDBC Driver.
01:50 And click on Add Library.
01:53 This will load the JDBC Driver.
01:56 Let us run the Projectas we had done earlier.
02:00 Now, type the User Name as "arya" and Password as "arya123*".
02:06 Then click on Sign In.
02:08 We can see the Success Greeting Page.
02:12 Click on here to logout.
02:15 Now, let us switch back to the IDE.
02:17 We will go to GreetingServlet dot java.
02:21 Come to the doPost method.
02:23 First, we get the username and password from the request using getParameter() method.
02:31 Next, we will see the code for JDBC connection.
02:35 First, we have initialized the Connection object, PreparedStatement object and ResultSet object to null.
02:44 Then we register the driver in our program.
02:48 Then we create a connection to the database.
02:52 Then, we execute the prepareStatement method on the Connection object.
02:58 We give the query to get the user details from Users table.
03:03 We check if username and password are same as that which are entered in the form.
03:09 Here, the question mark denotes each field in the database.
03:15 To supply values in place of question mark, we execute the setString() method.
03:22 We do this using the PreparedStatement object.
03:26 Then we execute the executeQuery method on the Prepared statement object.
03:33 We store the result in ResultSet object.
03:37 For successful login, we display the successGreeting page.
03:43 For this, we use the RequestDispatcher interface.
03:48 We use the getRequestDispatcher() method on the request to obtain the RequestDispatcher object.
03:56 We then invoke the forward() method on RequestDispatcher. object.
04:02 In this way, we forward to successGreeting dot jsp.
04:07 Now switch back to the slides.
04:10 Let us learn something about RequestDispatcher interface.
04:15 This interface provides the facility of dispatching the request to another resource.
04:22 The resource can be html, servlet or jsp.
04:26 Now, let us come back to the IDE.
04:29 Let us come to successGreeting dot'jsp.'
04:33 Here, we are displaying the success message You have successfully logged in.
04:38 Now, come back to the browser.
04:41 Type a username and password that we have not included in the database.
04:47 So, let me type abc as the username and abc123* as the password.
04:56 Then click on Sign In.
04:59 We can see that we get the error message on the same page itself.
05:03 "Please correct the following errors!!! Invalid username or password" .
05:09 Now, let us see the code for this.
05:12 So, switch back to the IDE.
05:14 Go to GreetingServlet dot java.
05:17 If the validation fails then we should display the error message.
05:22 First, we have initialized a List of errorMsgs.
05:27 We set the variable errorMsgs into the request scope using setAttribute method.
05:35 Here, errorMsgs is the attribute name.
05:39 We have initialized a String variable id to null.
05:44 Then, we check if the user exists in the database.
05:48 If yes, we store the value in the variable id.
05:53 Else, we add the error "Invalid username or password" to errorMsgs list.
06:00 If the errorMsgs list is not empty then we display the error messages on index dot jsp.
06:09 So, we have to redirect to index dot jsp.
06:13 We have already seen how to redirect to another page using RequestDispatcher.
06:20 Note that we have included this code inside the try catch block to handle exception scenarios.
06:27 Now, we will see how to fetch errorMsgs variable in index dot jsp.
06:34 First, we obtain the value of the attribute "errorMsgs".
06:38 This is done using getAttribute method on the request.
06:44 Note that we have included the Java code within the opening tag which is less than sign percentage sign and the closing tag percentage sign and greater than sign.
06:57 This block of code is known as scriptlet.
07:02 It contains Java code which is executed every time JSP is invoked.
07:08 If the value of errorMsgs is not null then we display this message.
07:15 "Please correct the following errors".
07:18 Then we iterate through the list of errorMsgs.
07:23 We then display the error messages as a list.
07:27 This is how we display the error messages on index dot jsp.
07:32 Now, let us see how to add a user into the database.
07:37 Before adding a user into the database, we have to create a model for the User table.
07:44 Now, let us see what a model is.
07:48 A model :Represents the underlying logical structure of data in a software application.
07:55 A Java class with attributes and setters and getters for them.
08:00 In this way, we can consider model as a whole instead of the individual attributes.
08:07 Now, switch back to Netbeans IDE.
08:11 I have already created the model User dot java.
08:16 Note that we have created this Java class inside the package org dot spokentutorial dot model.
08:24 We have the following attributes- firstName, surname, age, gender, email, username and password.
08:33 We have initialized them to empty values.
08:37 Then we have a parameterized constructor.
08:41 We also have the default constructor.
08:44 We define the getFirstName method.
08:47 We also define the setFirstName method.
08:51 Similarly, we define the set and get methods on each of the attributes.
08:57 Come back to the browser.
08:59 Now, let us click on here link to register.
09:03 Type all the fields in the Registration page.
09:07 Then click on Add User.
09:10 We get Add User Success page.
09:14 We get the message: "Your request to add harshita was successful".
09:20 Here harshita was the username that we have given.
09:24 Now, let us see how this is done.
09:28 So, switch back to the IDE.
09:30 Go to AddUserServlet dot java.
09:35 The steps are similar to that we had followed in GreetingServlet dot java.
09:40 First, we get the form parameters using the getParameter method.
09:46 We initialize variable user as instance of User model with individual attributes.
09:53 We set the variable user into the request scope using setAttribute method.
10:01 If there are no errors while filling the form, we execute the query to insert the values into the Users table.
10:10 Then we forward to successUser page.
10:15 Now, let us come to successUser dot jsp.
10:19 First, we have imported User dot java.
10:24 This line of code is called directive in JSP.
10:28 A JSP directive starts with opening tag- 'less than sign percentage sign and at the rate sign' and ends with closing tag- 'percentage sign and greater than sign'.
10:42 This one is a page directive.
10:45 The page directive contains a list of all imported packages.
10:50 We get the value of the attribute user and store it as the User object.
10:57 Then, we have the success message here.
11:00 Here, we have retrieved the Username.
11:04 We have used the getUsername() method on the request object.
11:09 We have done this using scriptlet tags.
11:12 Now, let us come back to the browser.
11:15 We will try to add a user already present in the database.
11:20 So, I will try to add harshita again.
11:24 We can see that we get the error message "Please correct the following errors!!!! Duplicate entry 'harshita' for key 'UserName'.
11:33 Now, let us register for a user once again.
11:37 Here, I have filled up the form now.
11:40 I have created a mistake in the Age field.
11:44 I have typed ab instead of a valid number.
11:48 Now, click on Add User.
11:51 We see, we get the error message- "The age must be a positive integer".
11:57 Now, let us see how this is done.
12:00 Switch back to the IDE.
12:03 Open AddUserServlet dot java.
12:08 Here also, we have created a list for errorMsgs.
12:11 Then we set the variable errorMsgs into the request scope using setAttribute method.
12:18 Then, we have declared ageUser of type integer and we have initialized it to -1 (minus one).
12:26 Inside the try catch block, we have used parseInt method.
12:31 This will return an integer, given a string representation of number as input.
12:37 So, here we validate that the age field contains a valid positive integer.
12:44 If the validation fails then we add error message to errorMsgs list.
12:51 The age must be a positive integer.
12:54 Similarly, we have to validate all the other fields to have valid data.
13:01 If the errorMsgs list is not empty then we will display the errorMsgs on addUser dot jsp itself.
13:09 We have already seen how to do this using the RequestDispatcher.
13:15 Now, let us come to addUser dot jsp.
13:19 Here also, first we have imported User dot java.
13:24 Inside the scriptlet tags, we have created an object of type User.
13:31 Then we get the value of the attribute errorMsgs using getAttribute method.
13:38 We check if this value is equal to null.
13:43 If it is not equal to null then we display the error message just as we had done for index dot jsp.
13:51 If not, we will get the value of the attribute user from request using 'User' model.
13:59 Then we have the form.
14:01 The form tag has action as AddUserServlet and method as POST.
14:07 The first field is First Name of input type as text , name as firstName and value as user dot getFirstName.
14:18 Here, we are initializing the value of firstName to empty string.
14:24 Similarly, you have to do for other fields.
14:28 We also have a submit button and value as Add User.
14:33 This is how we validate the fields in addUser.jsp.
14:38 You can try out different errors on "addUser" page.
14:42 Now, let us see if the user harshita has been added to the database.
14:49 So, come back to the Users table. We can see that harshita is added to the database.
14:56 In this tutorial, we have learnt:
14:58 Database connectivity and
15:00 Field validation.
15:02 To know more about the spoken tutorial project, watch the video available at the following link.
15:07 It summarizes the Spoken Tutorial Project.
15:11 If you do not have good bandwidth, you can download and watch it.
15:15 The Spoken Tutorial Project team:
15:17 Conducts workshops using spoken tutorials.
15:20 Gives certificates for those who pass an online test.
15:23 For more details, please write to: contact at spoken hyphen tutorial dot org.
15:29 Spoken Tutorial Project is a part of the "Talk to a Teacher Project".
15:32 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
15:38 More information on this mission is available at spoken-tutorial.org/NMEICT-intro
15:48 The Library Management System has been contributed by a leading software MNC through their "Corporate Social Responsibility Programme".
15:57 They have also validated the content for this spoken tutorial.
16:02 This is Arya Ratish from IIT Bombay, signing off.

Thank you for joining.

Contributors and Content Editors

Nancyvarkey, PoojaMoolya, Pratik kamble, Sandhya.np14, Shruti arya