J2EE Web Fundamentals Lesson 4 Request and Response

Preview:

DESCRIPTION

J2EE Web Fundamentals Lesson 4 Request and Response. Instructor: Dr. Segun Adekile. Outline. Objectives Course Text Book Basham, Bryan; Sierra, Kathy; Bates, Bert (2012-10-30). Head First Servlets and JSP (Kindle Location 1349). O'Reilly Media. Kindle Edition. Request and Response - PowerPoint PPT Presentation

Citation preview

J2EE Web FundamentalsLesson 4

Request and Response

Instructor: Dr. Segun Adekile

Outline

• Objectives• Course Text Book– Basham, Bryan; Sierra, Kathy; Bates, Bert (2012-

10-30). Head First Servlets and JSP (Kindle Location 1349). O'Reilly Media. Kindle Edition.

• Request and Response– Being a Servlet

Objectives

Agenda

• How the Container Handles a Request• Servlet Lifecycle– Loading– Initialization– Service• HTTP Methods• Request Object• Response Object

• Response Redirect• Request Dispatch

How the Container Handles a Request

How the Container Handles a Request

How the Container Handles a Request

Servlet Lifecycle• The servlet lifecycle is simple

• There is only one main state – initialized

• If the servlet isn’t initialized, then it’s either being initialized (running its constructor or init() method), being destroyed (running its destroy() method), or it simply does not exist

Servlet Lifecycle

Servlet Lifecycle

Servlet Lifecycle

Servlet Loading• When the Container starts, it looks for deployed web apps

and then starts searching for servlet class files– Finding the class is the first step– Loading the class is the second step, and it happens either on Container

startup or first client use. Your Container might give you a choice about class loading, or it might load the class whenever it wants.

Servlet Initialization• The constructor makes only an object• To be a servlet, the object needs to be granted servletness, getting all the unique privileges that come with being a servlet e.g. its ServletContext reference.

• You can override the init() method to add operations such as getting a db connection or registering yourself with other objects.

Servlet Initialization

Servlet Service: Request and Response

Servlet Service - HTTP Methods

Servlet Service - HTTP MethodsIntended use:

• for getting things

• Idempotent

Intended use:

• for sending data to be processed

• Non-idempotent

Servlet Service - HTTP Methods

Sending and using a single parameter

getParameterValues – for multiple values

Servlet Service – Request Object

Lifecycle key points

HTTPServletRequest key points

Servlet Service – Response Object• The response is what goes back to the client.• It is parsed and rendered by the browser.• Typically the response object is used to get an • output stream which is used to write the HTML, which goes back to the client.

Response Redirect

Response Redirect

Request Dispatch

HTTPServletResponse key points