Difference between revisions of "Java-Business-Application/C2/Java-servlets-and-JSPs/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 397: Line 397:
 
| style="border:1pt solid #000000;padding:0.097cm;"| We see the message '''Welcome''' in a different format.
 
| style="border:1pt solid #000000;padding:0.097cm;"| We see the message '''Welcome''' in a different format.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, come back to the '''IDE'''.
  
 
|-
 
|-

Latest revision as of 12:03, 23 August 2013

Title of script: Java Servlets and JSP

Author: arya

Keywords: web server,web container, servlets, jsp, video tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on Java Servlets and JSPs.
Slide 2 In this tutorial we will learn about:
  • Web server
  • Web container

We will also learn to create a simple Java Servlet and JSP.

Slide 3

Software Requirements

Here we are using
  • Ubuntu Version 12.04
  • Netbeans IDE 7.3
  • JDK 1.7
  • Firefox web-browser 21.0

You can use any web-browser of your choice.

Slide 4

Prerequisites

To follow this tutorial you must have knowledge of
  • Core Java using Netbeans IDE and
  • HTML

If not, for relevant tutorials please visit our website.

Slide 5

Web Server

Before moving onto Servlets and JSP, let us first understand a web server.
Slide 5

Web Server

A web server is a system that delivers content to end-users over the Internet.

It is also known as Internet server.

Slide 6

Web Container

A web container is a component of a web[1]server that interacts with Java servlets.


It is also known as servlet container.


The servlet container allows the servlets to execute inside it.

Switch to Netbeans. Now, let us learn how to write a simple servlet.


Switch to the Netbeans IDE.

We will now create a simple servlet and see.
Point to MyFirstProject in the Projects tab on the left hand side of the IDE. Click on the Projects tab on the left hand side of the IDE.


Earlier we had created a simple Project named MyFirstProject.


You can see it here on the left hand side of the IDE.

Let us now create a simple servlet inside this project.
Right click on MyFirstProject in the Projects tab. So, right-click on MyFirstProject.



Go to New->Servlets. Go to New and click on Servlets.
A New Servlet window opens. A New Servlet window opens.
Type the Class Name as MyServlet. Type the Class Name as MyServlet.
Type the Package Name as MyPackage. Type the Package Name as org.spokentutorial
Click on Next. Then click on Next.
Click on Add information to deployment descriptor(web.xml). Click on Add information to deployment descriptor (web.xml).
Point to Class Name. We can see that the Class Name is org.spokentutorial.MyServlet.
Point to Servlet Name. We can see that Servlet Name is same as that of the Class Name which is MyServlet.
Highlight the URL. Note that the URL pattern has the same name as the Class Name.
Change the URL name to MyServletPath. You can change it to MyServletPath.
Click on Finish. Then click on Finish.
Highlight the servlet name and package name in the Projects tab. The source code created by the IDE for MyServlet.java is seen in the Source Editor Window.
Move your mouse over the Source Editor Window. We see MyServlet.java is created in the package org.spokentutorial.
Highlight MyServlet.java Notice that a servlet is just like any other Java class.

Except that a servlet does not have a main method.

Slide 7


Glassfish server

Now, let us learn something about Glassfish server.

A servlet is deployed in a servlet container.

We are using Glassfish as our server.

Servlet container is a component of Glassfish that interacts with[2]servlets.

Switch back to Netbeans IDE Now, let us come back to Netbeans IDE.



Highlight extends HttpServlet. Note that MyServlet extends the HttpServlet.
Point to HttpServlet methods. At the bottom of the code, we can see HttpServlet methods.
Click on the plus sign on the left to view HttpServlet methods. Click on the plus sign on the left, to view these methods.
Highlight doGet and doPost methods. We see the methods - the doGet, doPost and getServletInfo methods.


We can override these methods.

Highlight processRequest method. We can see that there is one more method named processRequest at the top.
Delete processRequest and getServletInfo We will delete processRequest and getServletInfo methods to avoid confusion.
So we are left with two methods doGet and doPost.
Highlight doGet method. For now, we will look at the doGet method.
Highlight doGet method. doGet is the default method for any simple URL request.
So we will type some code inside the doGet method.
Remove the method call from both doGet and doPost methods. We had already deleted processRequest method.

So, remove the method call for processRequest method.

Also remove it from the doPost method.



Point to doGet method Now, let us come to the doGet method

We can see that there are two parameters that are passed to the doGet method.

Highlight request and response parameters of the doGet method. One is the request and the other is the response object.
Highlight request object. Also notice that, request is of type HttpServletRequest.
Highlight response object. And response object is of type HttpServletResponse.
We will use the response object to send the HTML response back to the client side.
For that, we will have to create a PrintWriter object.
Highlight the import statement. Notice that the PrintWriter class is already imported.
Type PrintWriter writer=response.getWriter(); So, inside the doGet method type

PrintWriter space writer equal to response dot getWriter open and close brackets semicolon


Press Enter.

On the next line type -

writer dot println within brackets and double quotes welcome.

Press Ctrl + S. Press Ctrl plus S to save the file.
Now, let us run the servlet.
Right click on MyServlet.java. So on the left hand side, in the Projects tab right click on MyServlet dot java.
Click on Run File. Click on Run File.

Point to Set Servlet Execution URI dialog box.


We get a Set Servlet Execution URI dialog box.



Click on OK.


Click on OK.



When the browser window opens, look at the URL.



Highlight the URL

Point to the Project name and servlet name.

It is localhost colon 8080 slash MyFirstProject slash MyServletPath.

Switch to Netbeans IDE


Here MyFirstProject is the context name and MyServletPath is the URL Pattern that we had set.
Highlight welcome We see the text welcome printed on the browser.
Switch to netbeans IDE Now go back to the netbeans IDE.
Point to print method. In the println method we can pass html code.
Type:

<h3>welcome</h3>

For example,

put welcome in h3 tag.

Press CTRL + S Save the file.
Since we deployed this servlet earlier, we need not run it again.


The web container automatically detects it.

Switch to the browser. So, we can go back to the browser.
Press F5. Refresh.


Show the message Welcome. We see the message Welcome in a different format.
Now, come back to the IDE.
Thus, we have successfully created a servlet.
We can create any web application using servlets.
We used the servlet to display an HTML page.
Highlight the println statements. Notice that, we have HTML code inside the Java code.
Even though this is possible, it is difficult to do for large web applications.


And hence not a recommended practice.

It would be better to replace this using JSP (Java Server Pages.)
Slide 7


Servlets and JSPs

We will see the use of servlets and jsps.
  • Servlets and JSPs are used together to separate presentation from content.
  • Servlets act as the controller and JSPs act as the view i.e the front-end.
  • Servlets contain HTML code within Java code.
  • JSPs contain Java code within HTML code.

We will learn more about these in the coming tutorials.

Switch back to Netbeans IDE.


Let us switch back to Netbeans IDE.


We will now create a JSP page.



Right Click on the MyFirstProject


Right click on MyFirstProject.




Cick on New. Go to New.
Click on JSP. Then click on JSP.
A new JSP window opens.
File name type: welcome Type the filename as welcome .
Click Finish And click on Finish.
Click on the Projects tab on the left hand side. Click on the Projects tab on the left hand side of the IDE.
Highlight Welcome.jsp. We can see that Welcome.jsp is under Web Pages folder.
Change Hello World to Welcome In the editor, change Hello World to Welcome.

Notice that Welcome is within h1 tags.

Press CTRL + S Save the file.
Go to the browser Go to the browser.
Type

welcome.jsp

and

Press Enter

In the url after MyFirstProject slash type welcome.jsp

And hit Enter.

Highlight the output We see the output Welcome.
Therefore for presentation purpose jsp is preferred.
Slide 8

Summary

Let us summarize

In this tutorial we have learnt

  • About web server and web container
  • To create a simple servlet
  • To create a simple jsp


Please make sure that you have completed this tutorial before proceeding further.
Slide 9


About slide

* Watch the video available at the following link
  • It summarizes the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Slide 10

About slide

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact at spoken hyphen tutorial dot org


Slide 11

Acknowledgment

Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Slide 12

Contributor slide

The Library Management System has been contributed by a leading software MNC, through their Corporate Social

Responsibility programme.


They have also validated the content for this spoken tutorial.


This is Arya Ratish from IIT Bombay signing off.


Thank you for joining.

Contributors and Content Editors

Arya Ratish