Java-Business-Application/C2/Database-and-validation/English-timed
From Script | Spoken-Tutorial
Revision as of 17:36, 7 July 2015 by Sandhya.np14 (Talk | contribs)
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: |
00:13 | * 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 AddLibrary. |
01:53 | This will load the JDBC Driver. |
01:56 | Let us run the Projectas we had done earlier. |
02:00 | Now, type the username as arya and password as arya*123. |
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 error!!! 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 : |
07:49 | * 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 initialize it to -1. |
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 Add User page. |
14:42 | Now, let us see if the user harshita has been added to the database. |
14:49 | So, come back to the user 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