1 00:00:01 --> 00:00:05 Welcome to the spoken-tutorial on Java Servlets and JSPs. 2 00:00:06 --> 00:00:08 In this tutorial we will learn about: 3 00:00:09 --> 00:00:09 Web server 4 00:00:10 --> 00:00:11 Web container 5 00:00:12 --> 00:00:17 We will also learn to create a simple Java Servlet and JSP 6 00:00:18 --> 00:00:19 Here we are using 7 00:00:20 --> 00:00:22 Ubuntu Version12.04 8 00:00:23 --> 00:00:26 Netbeans IDE 7.3 9 00:00:27 --> 00:00:28 JDK 1.7 10 00:00:29 --> 00:00:32 Firefox web-browser 21.0 11 00:00:33 --> 00:00:36 You can use any web-browser of your choice. 12 00:00:37 --> 00:00:40 To follow this tutorial you must have knowledge of 13 00:00:41 --> 00:00:44 Core Java using Netbeans IDE and 14 00:00:45 --> 00:00:46 HTML 15 00:00:47 --> 00:00:51 If not, for relevant tutorials please visit our website. 16 00:00:52 --> 00:00:57 Before moving onto Servlets and JSP, let us first understand a web server. 17 00:00:58 --> 00:01:04 A web server is a system that delivers content to end-users over the Internet. 18 00:01:05 --> 00:01:09 It is also known as Internet server. 19 00:01:10 --> 00:01:17 A web container is a component of the web server that interacts with Java servlets 20 00:01:18 --> 00:01:21 It is also known as servlet container. 21 00:01:22 --> 00:01:27 The servlet container allows the servlets to execute inside it. 22 00:01:28 --> 00:01:31 Now, let us learn how to write a simple servlet. 23 00:01:32 --> 00:01:34 Switch to the Netbeans IDE. 24 00:01:35 --> 00:01:39 Click on the Project tab on the left hand side of the IDE. 25 00:01:40 --> 00:01:45 Earlier we had created a simple Project named MyFirstProject. 26 00:01:46 --> 00:01:49 You can see it here on the left hand side of the IDE. 27 00:01:50 --> 00:01:54 Let us now create a simple servlet inside this project. 28 00:01:55 --> 00:01:58 So, right-click on MyFirstProject. 29 00:01:59 --> 00:02:02 Go to New and click on Servlet. 30 00:02:03 --> 00:02:04 A New Servlet window opens. 31 00:02:05 --> 00:02:08 Type the Class Name as MyServlet. 32 00:02:09 --> 00:02:15 Type the Package Name as org.spokentutorial 33 00:02:16 --> 00:02:17 Then click on Next. 34 00:02:18 --> 00:02:22 Click on Add information to deployment descriptor (web.xml). 35 00:02:23 --> 00:02:29 We can see that the Class Name is org.spokentutorial.MyServlet. 36 00:02:30 --> 00:02:36 We can see that Servlet Name is same as that of the Class Name which is MyServlet. 37 00:02:37 --> 00:02:44 Note that the URL pattern is the same name as that of the Class Name. i.e MyServlet 38 00:02:45 --> 00:02:49 We can change it to MyServletPath. 39 00:02:50 --> 00:02:52 Then click on Finish. 40 00:02:53 --> 00:03:00 The source code created by the IDE for MyServlet.java is seen in the Source Editor Window. 41 00:03:01 --> 00:03:08 We see MyServlet.java is created in the package org.spokentutorial. 42 00:03:09 --> 00:03:13 Notice that a servlet is just like any other Java class. 43 00:03:14 --> 00:03:18 Except that a servlet does not have a main method. 44 00:03:19 --> 00:03:23 Now, let us learn something about Glassfish server. 45 00:03:24 --> 00:03:27 A servlet is deployed in a servlet container. 46 00:03:28 --> 00:03:31 We are using Glassfish as our server. 47 00:03:32 --> 00:03:38 Servlet container is a component of Glassfish that interacts with servlets. 48 00:03:39 --> 00:03:41 Now, let us come back to Netbeans IDE. 49 00:03:42 --> 00:03:47 Note that MyServlet extends the HttpServlet. 50 00:03:48 --> 00:03:53 At the bottom of the code, we can see HttpServlet methods. 51 00:03:54 --> 00:03:58 Click on the plus sign on the left, to view these methods. 52 00:03:59 --> 00:04:08 We see the methods - doGet, doPost and getServletInfo methods. 53 00:04:09 --> 00:04:11 We can override these methods. 54 00:04:12 --> 00:04:17 We can see that there is one more method named processRequest at the top. 55 00:04:18 --> 00:04:24 We will delete processRequest and getServletInfo methods to avoid confusion. 56 00:04:25 --> 00:04:30 So we are left with two methods doGet and doPost. 57 00:04:31 --> 00:04:34 For now, we will look at the doGet method. 58 00:04:35 --> 00:04:40 doGetis the default method for any simple URLrequest. 59 00:04:41 --> 00:04:44 So we will type some code inside the doGet method. 60 00:04:45 --> 00:04:48 We had already deleted processRequest method. 61 00:04:49 --> 00:04:53 So,remove the method call for processRequest method. 62 00:04:54 --> 00:04:57 Also remove it from the doPost method. 63 00:04:58 --> 00:05:00 Now, let us come to the doGet method 64 00:05:01 --> 00:05:06 We can see that there are two parameters that are passed to the doGet method. 65 00:05:07 --> 00:05:11 One is the request and the other is the response object. 66 00:05:12 --> 00:05:17 Also notice that, request is of type HttpServletRequest. 67 00:05:18 --> 00:05:21 And response object is of type HttpServletResponse. 68 00:05:22 --> 00:05:29 We will use the response object to send the HTML response back to the client side. 69 00:05:30 --> 00:05:34 For that, we will have to create a PrintWriter object. 70 00:05:35 --> 00:05:39 Notice that the PrintWriter class is already imported. 71 00:05:40 --> 00:05:56 So, inside the doGet method type PrintWriter space writer equal to responsedot getWriter open and close brackets semicolon 72 00:05:57 --> 00:05:58 Press Enter. 73 00:05:59 --> 00:06:01 On the next line type - 74 00:06:02 --> 00:06:08 writer dot println within brackets and double quotes welcome. 75 00:06:09 --> 00:06:13 Then, Press Ctrl S to save the file. 76 00:06:14 --> 00:06:16 Now, let us run the servlet. 77 00:06:17 --> 00:06:23 So on the left hand side, in the Projects tab right click on MyServlet' dot java. 78 00:06:24 --> 00:06:26 Then, Click on Run File. 79 00:06:27 --> 00:06:31 We get a Set Servlet Execution URI dialog box. 80 00:06:32 --> 00:06:34 Click on OK. 81 00:06:35 --> 00:06:38 When the browser window opens, look at the URL. 82 00:06:39 --> 00:06:46 It is localhost colon 8080 slash MyFirstProject slash MyServletPath. 83 00:06:47 --> 00:06:54 Here MyFirstProject is the context name and MyServletPath is the URL Pattern that we had set. 84 00:06:55 --> 00:06:59 We see the text welcome printed on the browser. 85 00:07:00 --> 00:07:02 Now go back to the Netbeans IDE. 86 00:07:03 --> 00:07:06 In the println method we can pass html code. 87 00:07:07 --> 00:07:11 For example, put welcome in h3 tag. 88 00:07:12 --> 00:07:13 Now,Save the file. 89 00:07:14 --> 00:07:19 Since we deployed this servlet earlier, we need not run it again. 90 00:07:20 --> 00:07:22 The web container automatically detects it. 91 00:07:23 --> 00:07:26 So, we can go back to the browser. 92 00:07:27 --> 00:07:27 Refresh. 93 00:07:28 --> 00:07:31 We see the message Welcome in a different format. 94 00:07:32 --> 00:07:34 Now, come back to the IDE. 95 00:07:35 --> 00:07:38 Thus, we have successfully created a servlet. 96 00:07:39 --> 00:07:44 We can create any web application using servlets. 97 00:07:45 --> 00:07:48 We used the servlet to display an HTML code. 98 00:07:49 --> 00:07:53 Notice that, we have HTML code inside the Java code. 99 00:07:54 --> 00:07:59 Even though this is possible, it is difficult to do for large web applications. 100 00:08:00 --> 00:08:02 And hence not a recommended practice. 101 00:08:03 --> 00:08:09 It would be better to replace this using JSP (Java Server Pages.) 102 00:08:10 --> 00:08:12 We will see the use of servlets and JSPs. 103 00:08:13 --> 00:08:19 Servlets and JSPs are used together to separate presentation from content. 104 00:08:20 --> 00:08:24 Servlets act as the controller and JSPs act as the view 105 00:08:25 --> 00:08:29 Servlets contain HTML code inside Java code. 106 00:08:30 --> 00:08:34 JSPs contain Java code inside HTML code. 107 00:08:35 --> 00:08:38 We will learn more about these in the coming tutorials. 108 00:08:39 --> 00:08:41 Now, Switch back to Netbeans IDE. 109 00:08:42 --> 00:08:46 We will now create a simple JSP page. 110 00:08:47 --> 00:08:49 So, Right click on MyFirstProject. 111 00:08:50 --> 00:08:50 Go to New. 112 00:08:51 --> 00:08:53 and click on JSP. 113 00:08:54 --> 00:08:56 A new JSP window opens. 114 00:08:57 --> 00:09:00 Type the Filename as welcome . 115 00:09:01 --> 00:09:03 And then click on Finish. 116 00:09:04 --> 00:09:06 Click on the Projects tab on the left hand side 117 00:09:07 --> 00:09:12 We can see that Welcome.jsp is under Web Pages folder. 118 00:09:13 --> 00:09:18 Now, In the editor, change Hello World to Welcome. 119 00:09:19 --> 00:09:22 Notice that Welcome is within h1 tags. 120 00:09:23 --> 00:09:24 Now, Save the file. 121 00:09:25 --> 00:09:26 Come back to the browser. 122 00:09:27 --> 00:09:34 In the url after MyFirstProject slashtype welcome.jsp 123 00:09:35 --> 00:09:37 We see the output Welcome. 124 00:09:38 --> 00:09:41 Therefore for presentation purpose JSP is preferred. 125 00:09:42 --> 00:09:43 Let us summarize 126 00:09:44 --> 00:09:46 In this tutorial we have learnt 127 00:09:47 --> 00:09:48 About web server and web container 128 00:09:49 --> 00:09:51 To create a simple servlet 129 00:09:52 --> 00:09:54 To create a simple JSP 130 00:09:55 --> 00:10:00 Please make sure that you have completed this tutorial before proceeding further. 131 00:10:01 --> 00:10:03 Watch the video available at the following link 132 00:10:04 --> 00:10:07 It summarizes the Spoken Tutorial project 133 00:10:08 --> 00:10:12 If you do not have good bandwidth, you can download and watch it 134 00:10:13 --> 00:10:14 The Spoken Tutorial Project Team 135 00:10:15 --> 00:10:18 Conducts workshops using spoken tutorials 136 00:10:19 --> 00:10:21 Gives certificates for those who pass an online test 137 00:10:22 --> 00:10:27 For more details, please write to contact at spoken hyphen tutorial dot org 138 00:10:28 --> 00:10:31 Spoken Tutorial Project is a part of the Talk to a Teacher project 139 00:10:32 --> 00:10:39 It is supported by the National Mission on Education through ICT, MHRD, Government of India 140 00:10:40 --> 00:10:49 More information on this Mission is available at http://spoken-tutorial.org/NMEICT- Intro 141 00:10:50 --> 00:10:59 The Library Management System has been contributed by a leading software MNC, through their Corporate Social Responsibility program 142 00:11:00 --> 00:11:03 They have also validated the content for this spoken tutorial. 143 00:11:04 --> 00:11:09 This is Arya Ratish from IIT Bombay signing off. Thank you for joining.