Saturday, July 7, 2007

HTTP Status Codes

In a web app, whenever there is a communication between the browser (i.e client) and (web) server, there is a back-and-forth transfer of data. The client sends data to Server for processing. Data is then processed at the server side, and a response is sent back to client, indicating the status of the sent request. The response sent by Server to browser, typically, has a status line, some response headers, blank line, and the document, e.g.

HTTP/1.1 200 OK
Content-Type: text/plain

This is an example of status codes.

Here, the first line indicates the HTTP Version (HTTP/1.1), a status code (200) and a short description of status code (ok). Second line consists of the various headers sent by Server. At mimimal, it has a content type. The data after the blank line indicates the document itself, though not all responses may have a document. If a document is present, the content type indicates its MIME type.

Given below is a list of the commonly used status codes. The status codes are all three-digit numbers that are grouped by the first digit into 5 groups.

1xx: Informational

100 Continue
Continue with partial request.

101 Switching Protocols
Server will comply with Upgrade header and change to different protocol.


2xx: Successful

200 OK
Means that the server did whatever the client wanted it to, and all is well.

Others
The rest of the 2xx status codes are mainly meant for script processing and are
not often used.


3xx: Redirection
Means that the resource is somewhere else and that the client should try again at
a new address.

301 Moved permanently
The resource the client requested is somewhere else, and the client should go there
to get it. Any links or other references to this resource should be updated.

302 Moved temporarily
This means the same as the 301 response, but links should now not be updated, since
the resource may be moved again in the future.

304 Not modified
This response can be returned if the client used the if-modified-since header field
and the resource has not been modified since the given time. Simply means that the
cached version should be displayed for the user.


4xx: Client error
Means that the client screwed up somehow, usually by asking for something it should
not have asked for.

400 Bad request
The request sent by the client didn't have the correct syntax.

401 Unauthorized
Client tried to access password-protected page without proper authorization. Response should include a WWW-Authenticate header that the browser would use to pop up a username/password dialog box, which then comes back via the Authorization header.

403 Forbidden
The client is not allowed to access the resource and authorization will not help.
Often the result of bad file or directory permissions on the server.

404 Not found
Seen this one before? :) It means that the server has not heard of the resource
and has no further clues as to what the client should do about it. In simpler
words: dead link.

405 Method Not Allowed
The request method not allowed for this particular resource.


5xx: Server error
This means that the server screwed up or that it couldn't do as the client requested.

500 Internal server error
Something went wrong inside the server. It is often the result of Servlets or CGI
programs or (Any Other Server Side Technology) that crash or return improperly
formatted headers.

501 Not implemented
The request method is not supported by the server.

503 Service unavailable
This sometimes happens if the server is too heavily loaded and cannot service the
request. Usually, the solution is for the client to wait a while and try again.
A servlet might return this header if some thread or database connection pool is
currently full. Server can supply a Retry-After header.

505 HTTP Version Not Supported
Server doesn't support version of HTTP indicated in request line.


Just keep in mind that some of the status codes may be available only in HTTP/1.1 version though some of the browsers may still support only HTTP/1.0 version.
The general method of setting status codes is simply to call response.setStatus(int), there are two common cases where a shortcut method in HttpServletResponse is provided. The sendError method generates a 404 response along with a short message formatted inside an HTML document. The advantage of sendError over setStatus is that, with sendError, the server automatically generates an error page showing the error message. And the sendRedirect method generates a 302 response along with a Location header indicating the URL of the new document.


Advertisement
Fund Literacy, Care for the Environment, and get a Fair Price on the Books you Want.
http://www.betterworld.com/

No comments:

Total Pageviews

Reading List - 05-Mar-23

Talking to #AI may be the most important job skill of this century ( JohnGoodPasture ) Role of account management over the years. It’s a ro...