Difference between revisions of "Java-Business-Application/C2/Java-servlets-and-JSPs/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 9: | Line 9: | ||
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial we will learn about: | + | | In this tutorial, we will learn about: |
|- | |- | ||
Line 17: | Line 17: | ||
|- | |- | ||
| 00:10 | | 00:10 | ||
− | | ''' Web container''' | + | | ''' Web container'''. |
|- | |- | ||
| 00:12 | | 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:18 | | 00:18 | ||
− | |Here we are using | + | |Here we are using: |
|- | |- | ||
Line 41: | Line 41: | ||
|- | |- | ||
| 00:29 | | 00:29 | ||
− | | '''Firefox''' web-browser 21.0 | + | | '''Firefox''' web-browser 21.0. |
|- | |- | ||
Line 49: | Line 49: | ||
|- | |- | ||
|00:37 | |00:37 | ||
− | | To follow this tutorial you must have knowledge of | + | | To follow this tutorial, you must have knowledge of |
|- | |- | ||
Line 57: | Line 57: | ||
|- | |- | ||
| 00:45 | | 00:45 | ||
− | | '''HTML''' | + | | '''HTML'''. |
|- | |- | ||
Line 77: | Line 77: | ||
|- | |- | ||
| 01:10 | | 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'''. |
|- | |- | ||
Line 129: | Line 129: | ||
|- | |- | ||
| 02:09 | | 02:09 | ||
− | | Type the '''Package Name '''as '''org.spokentutorial''' | + | | Type the '''Package Name '''as '''org.spokentutorial'''. |
|- | |- | ||
Line 213: | Line 213: | ||
|- | |- | ||
| 04:09 | | 04:09 | ||
− | |We can override these methods. | + | |We can '''override''' these methods. |
|- | |- | ||
Line 225: | Line 225: | ||
|- | |- | ||
| 04:25 | | 04:25 | ||
− | | So we are left with two methods '''doGet''' and '''doPost.''' | + | | So, we are left with two methods '''doGet''' and '''doPost.''' |
|- | |- | ||
Line 245: | Line 245: | ||
|- | |- | ||
| 04:49 | | 04:49 | ||
− | |So,remove the method call for '''processRequest method.''' | + | |So, remove the method call for '''processRequest method.''' |
|- | |- | ||
Line 265: | Line 265: | ||
|- | |- | ||
| 05:12 | | 05:12 | ||
− | | Also notice that | + | | Also notice that '''request '''is of type '''HttpServletRequest.''' |
|- | |- | ||
Line 285: | Line 285: | ||
|- | |- | ||
| 05:40 | | 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''' |
|- | |- | ||
Line 293: | Line 293: | ||
|- | |- | ||
| 05:59 | | 05:59 | ||
− | | On the next line type | + | | On the next line type: |
|- | |- | ||
| 06:02 | | 06:02 | ||
− | |'''writer ''dot ''println ''within brackets and double quotes ''welcome.''' | + | |'''writer '''dot '''println '''within brackets and double quotes '''welcome.''' |
|- | |- | ||
|06:09 | |06:09 | ||
− | | Then, Press '''Ctrl S''' to save the file. | + | | Then, Press '''Ctrl S''' to '''save''' the file. |
|- | |- | ||
| 06:14 | | 06:14 | ||
− | | Now, let us run the '''servlet.''' | + | | Now, let us '''run''' the '''servlet.''' |
|- | |- | ||
| 06:17 | | 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:24 | | 06:24 | ||
− | | Then, | + | | Then, click on '''Run File.''' |
|- | |- | ||
Line 333: | Line 333: | ||
|- | |- | ||
| 06:47 | | 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. |
|- | |- | ||
Line 345: | Line 345: | ||
|- | |- | ||
| 07:03 | | 07:03 | ||
− | | In the '''println''' method we can pass '''html''' code. | + | | In the '''println''' method, we can pass '''html''' code. |
|- | |- | ||
Line 357: | Line 357: | ||
|- | |- | ||
| 07:14 | | 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. |
|- | |- | ||
Line 393: | Line 393: | ||
|- | |- | ||
| 07:49 | | 07:49 | ||
− | | Notice that | + | | Notice that we have '''HTML''' code inside the '''Java code.''' |
|- | |- | ||
Line 417: | Line 417: | ||
|- | |- | ||
| 08:20 | | 08:20 | ||
− | | '''Servlets '''act as the '''controller '''and '''JSPs '''act as the '''view ''' | + | | '''Servlets '''act as the '''controller '''and '''JSPs '''act as the '''view '''. |
|- | |- | ||
Line 441: | Line 441: | ||
|- | |- | ||
| 08:47 | | 08:47 | ||
− | | So, | + | | So, right click on '''MyFirstProject'''. |
|- | |- | ||
| 08:50 | | 08:50 | ||
− | | Go to '''New | + | | Go to '''New''' |
|- | |- | ||
Line 465: | Line 465: | ||
|- | |- | ||
| 09:04 | | 09:04 | ||
− | | Click on the '''Projects tab ''' | + | | Click on the '''Projects tab '''. On the left hand side, |
|- | |- | ||
Line 473: | Line 473: | ||
|- | |- | ||
| 09:13 | | 09:13 | ||
− | | Now, | + | | Now, in the editor, change '''Hello World''' to '''Welcome'''. |
|- | |- | ||
Line 489: | Line 489: | ||
|- | |- | ||
| 09:27 | | 09:27 | ||
− | | In the url after '''MyFirstProject''' '''slash'''type '''welcome.jsp''' | + | | In the url, after '''MyFirstProject''' '''slash'''type '''welcome.jsp''' |
|- | |- | ||
Line 501: | Line 501: | ||
|- | |- | ||
| 09:42 | | 09:42 | ||
− | | Let us summarize | + | | Let us summarize. |
|- | |- | ||
| 09:44 | | 09:44 | ||
− | |In this tutorial we have learnt | + | |In this tutorial, we have learnt: |
|- | |- | ||
| 09:47 | | 09:47 | ||
− | | About '''web server''' and '''web container''' | + | |* About '''web server''' and '''web container''' |
|- | |- | ||
| 09:49 | | 09:49 | ||
− | | To create a '''simple servlet''' | + | |* To create a '''simple servlet''' |
|- | |- | ||
| 09:52 | | 09:52 | ||
− | | To create a '''simple JSP''' | + | |* To create a '''simple JSP'''. |
|- | |- | ||
Line 525: | Line 525: | ||
|- | |- | ||
| 10:01 | | 10:01 | ||
− | | Watch the video available at the following link | + | | Watch the video available at the following link. |
|- | |- | ||
| 10:04 | | 10:04 | ||
− | | It summarizes the Spoken Tutorial project | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 10:08 | | 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:13 | |10:13 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: |
|- | |- | ||
Line 545: | Line 545: | ||
|- | |- | ||
| 10:19 | | 10:19 | ||
− | | Gives certificates for those who pass an online test | + | | Gives certificates for those who pass an online test. |
|- | |- | ||
| 10:22 | | 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:28 | | 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:32 | | 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:40 | | 10:40 | ||
− | | More information on this | + | | More information on this mission is available at http://spoken-tutorial.org/NMEICT- Intro |
|- | |- | ||
| 10:50 | | 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. |
|- | |- | ||
Line 573: | Line 573: | ||
|- | |- | ||
| 11:04 | | 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 21:33, 13 June 2015
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 method type: PrintWriter space writer equal to responsedot 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. |