Title of script: Servlet Methods
Keywords: servlets, video tutorial, GET, POST
|Slide 1||Welcome to the spoken-tutorial on Servlet Methods.|
|Slide 2|| In this tutorial we will learn to
| Slide 3
| Here we are using
You can use any web-browser of your choice.
| Slide 4
| To follow this tutorial you must know
If not, for relevant tutorials please visit our website.
|Slide 5|| We will begin by creating our web application- 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.
| This form has two input elements - Username and Password.
|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.
|Slide 6|| Now, we will see what the servlet does as a controller.
|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.
|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.
POST Method is used when:
| Slide 9
| Let us summarize.
In this tutorial we have learnt to:
|Please make sure that you have completed this tutorial before proceeding further.|
| Slide 10
| Watch the video available at the following link.
| Slide 11
| The Spoken Tutorial Project Team
| Slide 12
| Spoken Tutorial Project is a part of the Talk to a Teacher Project
| Slide 13
| The Library Management System has been contributed by a leading software MNC, through their Corporate Social Responsibility Programme.