Java-Business-Application/C2/Java-servlets-and-JSPs/English-timed
From Script | Spoken-Tutorial
| Time | Narration |
| 00:01 | Welcome to the spoken-tutorial on Java Servlets and JSPs. |
| 00:06 | In this tutorial, we will learn about: |
| 00:09 | Web server Web container. |
| 00:12 | We will also learn to create a simple Java Servlet and JSP. |
| 00:18 | Here we are using: |
| 00:20 | Ubuntu Version 12.04 |
| 00:23 | Netbeans IDE 7.3 |
| 00:27 | JDK 1.7 |
| 00:29 | Firefox web-browser 21.0. |
| 00:33 | You can use any web-browser of your choice. |
| 00:37 | To follow this tutorial, you must have knowledge of: |
| 00:41 | Core Java using Netbeans IDE and |
| 00:45 | HTML. |
| 00:47 | If not, for relevant tutorials please visit our website. |
| 00:52 | Before moving onto Servlets and JSP, let us first understand a web server. |
| 00:58 | A web server is a system that delivers content to end-users over the Internet. |
| 01:05 | It is also known as Internet server. |
| 01:10 | A web container is a component of the web server that interacts with Java servlets. |
| 01:18 | It is also known as servlet container. |
| 01:22 | The servlet container allows the servlets to execute inside it. |
| 01:28 | Now, let us learn how to write a simple servlet. |
| 01:32 | Switch to the Netbeans IDE. |
| 01:35 | Click on the Project tab on the left hand side of the IDE. |
| 01:40 | Earlier, we had created a simple Project named MyFirstProject. |
| 01:46 | You can see it here, on the left hand side of the IDE. |
| 01:50 | Let us now create a simple servlet inside this Project. |
| 01:55 | So, right-click on MyFirstProject. |
| 01:59 | Go to New and click on Servlet. |
| 02:03 | A New Servlet window opens. |
| 02:05 | Type the Class Name as MyServlet. |
| 02:09 | Type the Package name as org.spokentutorial. |
| 02:16 | Then click on Next. |
| 02:18 | Click on Add information to deployment descriptor (web.xml). |
| 02:23 | We can see that the Class Name is org.spokentutorial.MyServlet. |
| 02:30 | We can see that the Servlet Name is same as that of the Class Name which is MyServlet. |
| 02:37 | Note that the URL pattern is the same name as that of the Class Name. i.e MyServlet. |
| 02:45 | We can change it to MyServletPath. |
| 02:50 | Then click on Finish. |
| 02:53 | The source code created by the IDE for MyServlet.java is seen in the Source Editor window. |
| 03:01 | We see MyServlet.java is created in the package org.spokentutorial. |
| 03:09 | Notice that the servlet is just like any other Java class |
| 03:14 | except that the servlet does not have a main method. |
| 03:19 | Now, let us learn something about Glassfish Server. |
| 03:24 | A servlet is deployed in a servlet container. |
| 03:28 | We are using Glassfish as our server. |
| 03:32 | Servlet container is a component of Glassfish that interacts with servlets. |
| 03:39 | Now, let us come back to Netbeans IDE. |
| 03:42 | Note that MyServlet extends the HttpServlet. |
| 03:48 | At the bottom of the code, we can see HttpServlet methods. |
| 03:54 | Click on the plus sign on the left to view these methods. |
| 03:59 | We see the methods - doGet, doPost and getServletInfo methods. |
| 04:09 | We can override these methods. |
| 04:12 | We can see that there is one more method named processRequest at the top. |
| 04:18 | We will delete processRequest and getServletInfo methods to avoid confusion. |
| 04:25 | So, we are left with two methods doGet and doPost. |
| 04:31 | For now, we will look at the doGet method. |
| 04:35 | doGet is the default method for any simple URLrequest. |
| 04:41 | So, we will type some code inside the doGet method. |
| 04:45 | We had already deleted processRequest method. |
| 04:49 | So, remove the method call for processRequest method. |
| 04:54 | Also remove it from the doPost method. |
| 04:58 | Now, let us come to the doGet method |
| 05:01 | We can see that there are two parameters that are passed to the doGet method. |
| 05:07 | One is the request and the other is the response object. |
| 05:12 | Also notice that request is of type HttpServletRequest. |
| 05:18 | And response object is of type HttpServletResponse. |
| 05:22 | We will use the response object to send the HTML response back to the client side. |
| 05:30 | For that, we will have to create a PrintWriter object. |
| 05:35 | Notice that the PrintWriter class is already imported. |
| 05:40 | So, inside the doGet method type: PrintWriter space writer equal to response dot getWriter open and close brackets semicolon. |
| 05:57 | Press Enter. |
| 05:59 | On the next line, type: |
| 06:02 | writer dot println within brackets and double quotes Welcome. |
| 06:09 | Then, press Ctrl S to save the file. |
| 06:14 | Now, let us run the servlet. |
| 06:17 | So, on the left hand side, in the Projects tab right click on MyServlet dot java. |
| 06:24 | Then, click on Run File. |
| 06:27 | We get a Set Servlet Execution URI dialog box. |
| 06:32 | Click on OK. |
| 06:35 | When the browser window opens, look at the URL. |
| 06:39 | It is localhost colon 8080 slash MyFirstProject slash MyServletPath. |
| 06:47 | Here, MyFirstProject is the context name and MyServletPath is the URL pattern that we had set. |
| 06:55 | We can see the text Welcome printed on the browser. |
| 07:00 | Now go back to the Netbeans IDE. |
| 07:03 | In the println method, we can pass html code. |
| 07:07 | For example, put Welcome in h3 tag. |
| 07:12 | Now, save the file. |
| 07:14 | Since we deployed this servlet earlier, we need not run it again. |
| 07:20 | The web container automatically detects it. |
| 07:23 | So, we can go back to the browser. |
| 07:27 | Refresh.We see the message Welcome in a different format. |
| 07:32 | Now, come back to the IDE. |
| 07:35 | Thus, we have successfully created a servlet. |
| 07:39 | We can create any web application using servlets. |
| 07:45 | We used the servlet to display an HTML code. |
| 07:49 | Notice that we have HTML code inside the Java code. |
| 07:54 | Even though this is possible, it is difficult to do for large web applications. |
| 08:00 | And hence not a recommended practice. |
| 08:03 | It would be better to replace this using JSP or Java Server Pages. |
| 08:10 | We will see the use of servlets and JSPs. |
| 08:13 | Servlets and JSPs are used together to separate presentation from content. |
| 08:20 | Servlets act as the controller and JSPs act as the view . |
| 08:25 | Servlets contain HTML code inside Java code. |
| 08:30 | JSPs contain Java code inside HTML code. |
| 08:35 | We will learn more about these in the coming tutorials. |
| 08:39 | Now, switch back to Netbeans IDE. |
| 08:42 | We will now create a simple JSP page. |
| 08:47 | So, right click on MyFirstProject. |
| 08:50 | Go to New and click on JSP. |
| 08:54 | A New JSP window opens. |
| 08:57 | Type the Filename as welcome. |
| 09:01 | And then click on Finish. |
| 09:04 | Click on the Projects tab on the left hand side. |
| 09:07 | We can see that Welcome.jsp is under Web Pages folder. |
| 09:13 | Now, in the editor, change Hello World to Welcome. |
| 09:19 | Notice that Welcome is within "h1" tags. |
| 09:23 | Now, save the file. |
| 09:25 | Come back to the browser. |
| 09:27 | In the url, after MyFirstProject slash, type: welcome.jsp. |
| 09:35 | We see the output Welcome. |
| 09:38 | Therefore, for presentation purpose JSP is preferred. |
| 09:42 | Let us summarize. |
| 09:44 | In this tutorial, we have learnt: |
| 09:47 | About web server and web container. |
| 09:49 | To create a simple servlet. |
| 09:52 | To create a simple JSP. |
| 09:55 | Please make sure that you have completed this tutorial before proceeding further. |
| 10:01 | Watch the video available at the following link. |
| 10:04 | It summarizes the Spoken Tutorial project. |
| 10:08 | If you do not have good bandwidth, you can download and watch it. |
| 10:13 | The Spoken Tutorial Project Team: |
| 10:15 | Conducts workshops using spoken tutorials. |
| 10:19 | Gives certificates to those who pass an online test. |
| 10:22 | For more details, please write to: contact at spoken hyphen tutorial dot org. |
| 10:28 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
| 10:32 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
| 10:40 | More information on this mission is available at http://spoken-tutorial.org/NMEICT- Intro . |
| 10:50 | The Library Management System has been contributed by a leading software MNC through their "Corporate Social Responsibility" program. |
| 11:00 | They have also validated the content for this spoken tutorial. |
| 11:04 | This is Arya Ratish from IIT Bombay, signing off.
Thank you for joining. |