Class3 Servlets 1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Damodar Chetty

Todays Objectives
HTTP methods Servlet API Dynamic Environment Setup

Revisiting HTTP

Concepts
Connection Oriented Stateless Protocol
Persistent connection Uses TCP/IP sockets (well known port 80)

Combines protocol headers and payload

Revisiting HTTP

Safe Methods
Only request information
Do not change state on the server May return different responses

Idempotent Methods
Making a request multiple times has same effect Client can retry method on network/softare failure

Revisiting HTTP

Key HTTP Methods


GET
POST PUT
Safe : Yes. Safe : No. Idempotent: Yes. Idempotent: No.

Safe : No.
Safe : No.

Idempotent: Yes.
Idempotent: Yes.

DELETE

Spying on HTTP
liveHttpHeaders for Firefox

Fiddler for IE

Servlet API - Overview

Servlet
contraction of a server-let
Managed component deployed to a container provided with lifecycle and enterprise services Focus on high level abstractions
Requests Responses Sessions

Servlet API - Overview

Container Responsibilities
Knows HTTP Creates the request and response objects Determines the request handler servlet Loads/initializes the servlet, if first time Allocate a request processing thread Calls the servlets service() method Converts response object to HTTP response Deletes the request and response objects Destroys the servlet

Servlets Overview

States
Does not exist Initialized

Initialized servlet can access


ServletConfig One per servlet Initialization parameters from web.xml ServletContext One per web app Context parameters from web.xml Retrieve server information (version, JSR, etc.)

Servlets Overview Lifecycle

init()

contains initialization code called after instance created, but before service()

service()
called once per request called on its own request thread invokes doXXX() based on HTTP method

destroy()

Servlet Names

File path name


based on Fully Qualified Class Names located in WEB-INF/classes or WEB-INF/lib

Deployment name (Logical name)


Chosen by the deployer Indirection between filename and URL name

Public URL name


Name the client knows about
Used by container to locate the servlet Used in HTML forms and HTTP requests

Servlet Names

Advantages
Flexibility: classes can move without client changes Security: hides implementation details

Configuring a Servlet

Servlets Class Diagram

Request

This are interfaces

Response
Interface Protocol spesific Content specific text/html, image/jpeg,

PrintWriter writer = response.getWriter(); writer.println(text);

Interface

Protocol Adds HTTP specific options Used to return data to client

Development vs Deployment

You might also like