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