Easter Pastel Eggs - exercise

Download Report

Transcript Easter Pastel Eggs - exercise

Chapter 6
Generating the Server
Response: HTTP Status
Codes
Nguyen Thi Nhung
Contents
•
•
•
•
•
Format of the HTTP response
Common HTTP 1.1 Status Codes
How to set status codes
What the status codes are good for
Some examples
#
Introduction
HTTP Request:
“Get me the document called /index.html”
HTTP Response:
“Okay, here it is! It’s in HTML format”
Server
(w3c.org)
Client
HTTP Message
#
HTTP Request format
#
HTTP Response format
#
HTTP Message Examples
#
Status code classes
Overall range
Defined range
Category
100-199
100-101
Informational
200-299
200-206
Successful
300-399
300-307
Redirection
400-499
400-417
Client error
500-599
500-505
Server error
#
100 - Continue
Request message
Expect
= "Expect" ":" 1#expectation
expectation = "100-continue" | expectation-extension
expectation-extension = token [ "=" ( token | quoted-string )
*expect-params ]
expect-params = ";" token [ "=" ( token | quoted-string ) ]
Server
Request message
Client
HTTP/1.1 100 Continue
Request message
POST / HTTP/1.1
…..
Server
Request message
Client
HTTP/1.1 200 OK
#
200 – OK
• The request has succeeded.
• This status is default for servlet
#
300 – 399 Redirected request to new location
#
301 – Moved Permanently
#
302 – Found
#
404 – Not found
#
500 – Internal Server Error
#
Specifying Status Codes
• Code 200 is set automatically
• Use response.setStatus(int, String)
• Call setStatus() before returning any of the
content with PrintWriter
• Can use defined constant for readability
(SC_MOVED_TEMPORARILY,
SC_NOT_FOUND)
#
sendRedirect() method
• A shortcut to set 302 code
• Direct the browser to connect to a new
location
• Generate 302 code and Location header
giving the URL of new document
• response.sendRedirect("http://www.google
.com");
#
sendError() method
• A shortcut to set 400s code
• Used when no document is found on the
server
• Generate 400s code and a short message in
HTML format and send to client
• response.sendError(res.SC_NOT_FOUND,
”Sorry, the page cannot be found”);
#
Examples
•
•
•
•
WrongDestination.java
SearchEngines.java
SearchSpec.java
SearchUtilities.java
#
THANK YOU!