Difference between revisions of "Java-Business-Application/C2/Servlet-Methods/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Servlet Methods '''Author: arya ''' '''Keywords: servlets, video tutorial, GET, POST''' {| style="border-spacing:0;" | style="border:1pt solid #000000…')
 
Line 4: Line 4:
  
 
'''Keywords: servlets, video tutorial, GET, POST'''
 
'''Keywords: servlets, video tutorial, GET, POST'''
 
 
 
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
 
| style="border:1pt solid #000000;padding:0.097cm;"| <center>'''Visual Cue'''</center>
 
| style="border:1pt solid #000000;padding:0.097cm;"| <center>'''Visual Cue'''</center>

Revision as of 15:26, 25 June 2014

Title of script: Servlet Methods

Author: arya

Keywords: servlets, video tutorial, GET, POST

Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on Servlet Methods.
Slide 2 In this tutorial we will learn to
  • create a simple login form using JSP
  • Pass parameters using doGet method
  • Pass parameters using doPost method
  • Difference between doGet and doPost methods


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 know
  • Core Java using Netbeans IDE
  • HTML
  • Basics of Java Servlets and JSPs

If not, for relevant tutorials please visit our website.

Slide 5 We will begin by creating our web application- the Library Management System.


First, we will create the home page.


The home page will contain a simple login form.


It will allow authenticated users to login to the Library Management System.

Switch to Netbeans IDE. Now, let us switch to Netbeans IDE.
Switch to index.jsp page. Let us go to the index dot jsp page, that we had already modified earlier.
I have modified this page to create our home page.
Highlight the title. We keep the title as Home Page.
Highlight table and border=1 Inside the body, we have a table, with border equal to 1.

You can have a look at the code here.

Highlight Welcome to Library Management System. Inside the table, we have included a heading, Welcome to Library Management System.
Highlight This is the home page for Library Management System. Next, we have paragraph tag that includes, This is the home page for Library Management System.
Highlight the line containing hyperlink. Then, we have a hyperlink, which links to a page called visitorHomePage dot jsp.
We will create this page later.
Highlight form. Next,we have a very simple login form.

This form allows a registered user to login .

Before creating the form we will have to create a servlet named GreetingServlet.
Pause the tutorial and create a new servlet as explained in the earlier tutorial.
Note that the servlet name is GreetingServlet.


The URL pattern should be GreetingServletPath.

This form has two input elements - Username and Password.


It also has a Submit button that says Sign In.

Highlight paragraph tag. Next, we have a paragraph tag that includes a link to addUser.jsp.

This is the registration page for those users who have not yet registered.

Now, let us go to our GreetingServlet.java.
Highlight org.spokentutorial. Note that GreetingServlet.java is created in the same package org.spokentutorial.
Highlight request object. This servlet will be able to access the form data from the request object.
Point to MyServlet. This servlet will act as a controller.


Do you recall that we had come across controller earlier?

Slide 6 Now, we will see what the servlet does as a controller.


The form data will reside in the request object.


The first task is to retrieve the form data parameters.


This is done using the getParameter method on the request object.

Switch to Netbeans IDE. So, let us switch to Netbeans IDE.
Type PrintWriter out =response.getWriter(); Type inside the doGet method,

PrintWriter space out equal to response dot getWriter.

Next, we will retrieve the form data parameters.
Type String username=request.getParameter(“userName”);


So on the next line type,

String space username equal to request dot getParameter within brackets and double quotes userName semicolon.

Note that this userName is the name we have included in the form tag for User Name.

Type String password=request.getParameter(“password”); Similarly, we will retrieve the password also.

So on the next line, type, String space username equal to request dot getParameter within brackets and double quotes password semicolon.

Next, we will print the User Name in the output.
out.println(“Hello from GET Method” + username); So, on the next line type

out dot println within brackets and double quotes Hello from GET Method plus username.

Right click on MyFirstProject. <Pause>

To run this project, right-click on MyFirstProject.

Click on Clean and Build. Click on Clean and Build.
Right click on MyFirstProject and click on Run. Again right click on MyFirstProject and click on Run.
Hover your mouse over the console. The server is up and running.

It has deployed MyFirstProject.

Hover your mouse over the Home Page. Now, we have got our home page displayed in the browser.
Point the mouse to the title. Observe that the title of the page is Home Page.
Hover your mouse over the login form. We can see a very simple login form here.
Let me enter the Username and Password.
Type spoken in the textbox. I will type arya as the Username.
Type spoken*123 as the Password. And arya*123 as the Password.



Click on Sign In. Then click on Sign In.
Hover your mouse over the browser window. We can see that we have got the output Hello from GET Method arya.
Now, the user was able to login here because we have not included any validation inside the code. We will do this in the later tutorial.
Highlight the form data.

Highlight the question mark.

Now, have a look at the URL here.

It is localhost colon 8080 slash MyFirstProject slash GreetingServletPath question mark userName equal to arya&password equal to arya*123.

The form data is separated from the page information by a question mark.

Highlight userName=spoken&password=spoken*123 We can see the username and password that we had entered in the form is inside the URL also.
Now, let us try to do the same using POST Method.
Switch back to the IDE. Switch back to the IDE.
Copy the code we had written for the doGet Method and paste it in the doPost Method. Copy the code we had written for the doGet Method and paste in the doPost Method.
Change GET to POST. Now, change the println statement to Hello from POST Method.
Open index.jsp. Now, let us open index dot jsp.
Change GET to POST Here, we must change the method attribute of the form tag to POST.


You can have a look at this code now.


We have form action equal to GreetingServletPath method equal to POST.

Type method=”POST”. Now, we will run this Project again
Right click on MyFirstProject and click on Run
Highlight the output. We have got an output similar to the one we got, when we used the GET method.
So let us type the User Name and Password again.
Then click on Sign In.
Note that we have got Hello from POST Methodarya.
Now, take a look at the URL.

localhost colon 8080 slash MyFirstProject slash GreetingServletPath

Here we do not see the form data in the URL of the request.
This is the major difference between doGet and doPost Methods.
Slide 7

When to use GET and POST Methods?

Now, let us learn when to use GET and when to use POST Methods.


GET Method is used when:

  • the form is small and hence the data is less.
  • the user wants the contents of the data to be visible in the URL.

POST Method is used when:

  • the form is large and hence the data is more.
  • the user does not want the contents of the data to be visible in the URL.

ex: passwords

<Pause>

Slide 9

Summary

Let us summarize.

In this tutorial we have learnt to:

  • create a simple login form using JSP
  • Pass parameters using doGet method
  • Pass parameters using doPost method
  • Difference between doGet and doPost methods


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

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 11

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 12

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher Project


Slide 13

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, Nancyvarkey, PoojaMoolya