Java-Business-Application/C2/Java-servlets-and-JSPs/English
Title of script: Java Servlets and JSP
Author: arya
Keywords: web server,web container, servlets, jsp, video tutorial
|
|
Slide 1 | Welcome to the spoken-tutorial on Java Servlets and JSPs. |
Slide 2 | In this tutorial we will learn about:
We will also learn to create a simple Java Servlet and JSP. |
Slide 3
Software Requirements |
Here we are using
You can use any web-browser of your choice. |
Slide 4
Prerequisites |
To follow this tutorial you must have knowledge of
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.
|
Switch to Netbeans. | Now, let us learn how to write a simple servlet.
|
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.
|
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
|
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.
|
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
|
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.
| |
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.
| |
It would be better to replace this using JSP (Java Server Pages.) | |
Slide 7
|
We will see the use of servlets and jsps.
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
|
Please make sure that you have completed this tutorial before proceeding further. | |
Slide 9
|
* Watch the video available at the following link
|
Slide 10
About slide |
The Spoken Tutorial Project Team
|
Slide 11
Acknowledgment |
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Slide 12
Contributor slide |
The Library Management System has been contributed by a leading software MNC, through their Corporate Social
Responsibility programme.
|