Week2Lecture 153575

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

CS5054NI

ADVANCED PROGRAMMING
AND
TECHNOLOGIES
Week 2 - Lecture
Web pages and Servlets

ADVANCED PROGRAMMING AND TECHNOLOGIES Prithivi Maharjan


Learning Objectives
• Understand the basics of Web-page

• Know about different types of Web-page

• Describe about GET and POST Request

• Understand Java Servlets

Advanced Programming and Technologies August 7, 2022 | 2


Web Page
A Web-page is a document that is commonly, written in HTML, JSP or other languages.

Advanced Programming and Technologies August 7, 2022 | 3


Web Page
They are viewed in browsers such as google chrome, Mozilla Firefox and so on.

Advanced Programming and Technologies August 7, 2022 | 4


Accessing Web Page
• To access a web page, we type the name of the website, with .com (.com.np) at the end
(In some cases, it can also contains country as well along with .com)

Advanced Programming and Technologies August 7, 2022 | 5


Web Pages
• A web page is a document (hypertext) that a website provides on the web browser for display in front of
a site visitor.
• Many web pages link together on a website, and they may contain audio, images, videos, and so on

Advanced Programming and Technologies August 7, 2022 | 6


Types of Web Pages
There are two types of Web Pages
a. Static Webpage
b. Dynamic Webpage

Advanced Programming and Technologies August 7, 2022 | 7


Static Web Page
Login Pages

Advanced Programming and Technologies | 8


Static Web Page
The server locates this page, adds the HTTP headers, then immediately replies with an HTTP
response. This way, you can view these web pages through a web browser

Advanced Programming and Technologies August 7, 2022 | 9


Dynamic Web Page

Advanced Programming and Technologies | 10


Dynamic Web Page
• Dynamic websites are those that generate web pages in real-time.
• The web server parses the code, and the generated HTML is transmitted to the client's browser
when a client requests a dynamic page.

Advanced Programming and Technologies August 7, 2022 | 11


Client Server Model
• A web client lets the user request something on the server and shows the user the result of the
request
• A web server takes a client request and gives something back to the client.

Advanced Programming and Technologies | 12


Get Request
• It is the simplest HTTP method and its main job is to ask the server to get a resource and send
it back to the user.
• The data you send with a Get is appended to the URL up in a browser bar, so whatever we
send a get request, the information is exposed.

Advanced Programming and Technologies | 13


Anatomy of an HTTP GET Request

Advanced Programming and Technologies | 14


HTTP Methods

ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 15


HTTP response status codes

• Informational responses (100 – 199)


• Successful responses (200 – 299)
• Redirection messages (300 – 399)
• Client error responses (400 – 499)
• Server error responses (500 – 599)

| 16
HTTP response status codes

| 17
MySql Database Connectivity

ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 18


Java Enterprise Edition
• Provides a platform for developers with enterprise features such as distributed computing and
web services.
• Contexts where Java EE is used are e-commerce, accounting, banking information systems.

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 19


Java Enterprise Edition

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 20


Servlet

• Servlets are a component of JEE framework used for web


development. (JEE = Java Enterprise Edition)
• In short, they are responsible for accepting a request,
processing it, and sending a response back.

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 21


Servlet
• A Servlet consists of Servlet API (API = Application Programming Interface)
• The Servlet API consists of two important packages that encapsulates all the
important classes and interface, namely :

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 22


Life Cycle of Servlets

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 23


Servlet
• Servlet doesn’t have a main method.

• They are under the control of another java application called a container.

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 24


Need of Container
When a web server gets a request for servlet, the server hands the request to the container
in which the servlet is deployed.

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 25


Need of Container
Its the container that gives the servlet - the Http request and response (Container that calls
the servlet ‘s method ( doPost() or doGet() ).

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 26


How Servlet Works?
Step 1: User click a link that has a URL to a servlet

GET

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 27


How Servlet Works?
Step 2: The container sees that the request is for the servlet
So that container creates two objects:
1) HttpServletResponse 2) HttpServletRequest

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 28


How Servlet Works?
Step 3: The container finds the correct servlet based on the URL in the request, creates
or allocates a thread for that request, and passes the request and response object to the
servlet thread.

Container
Servlet

Request
Response

Thread

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 29


How Servlet Works?
Step 4: The container invokes the Servlet’s method. Depending on the type of request, the service
method calls either the doGet() or doPost() method.

For this example we will assume the request was an HTTP GET

Request

Response

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 30


How Servlet Works?
Step 5: The doGet() method generates the required data which is the response object for the page.

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 31


How Servlet Works?
Step 6: The thread completes, the container converts the response object into HTTP
Response and send back into the client.

No thread

Delete the request and


response object

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 32


Http Servlet Class Methods
1. doGet()
Used to handle server side GET request

2. doPost()
Used to handle server side POST request

ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 33


Http Servlet Class Methods
3. doPut()

4. doDelete()

5. doHead()

6. doOptions()

7. doTrace()

8. service()

ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 34


Tutorial
Create a new servlet in our project.

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 35


Workshop
Dynamic web application – Servlet task

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 36


Summary
1. We understood the concepts of static and dynamic pages.

2. Http methods and its response status codes.

3. We understood the concepts of servlets with its workflow and lifecycle.

4. We learned the importance of container in servlets.

5. We understood the information of HttpServlet class and methods

.ADVANCED PROGRAMMING AND TECHNOLOGIES August 7, 2022 | 37


Any Questions?
Thank You

You might also like