0% found this document useful (0 votes)
20 views

HTTP Response Codes

Uploaded by

Sripathi Ravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

HTTP Response Codes

Uploaded by

Sripathi Ravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

HTTP Request Headers

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletresponse;
// servlet implementation;
@WebServlet("/Headers")
public class Headers extends HttpServlet {
private static final long serialVersionUID = 1L;
public Headers() { super(); }
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String str = "Display Header Request";
out.println(
"<BODY BGCOLOR=\"#FF5732\">\n"
+ "<H1 ALIGN=CENTER>" + str + "</H1>\n"
+ "<B><center>Request Method: <center></B>"
+ request.getMethod() + "<BR>\n"
+ "<B>Request URI: </B>"
+ request.getRequestURI() + "<BR>\n"
+ "<B>Request Protocol: </B>"
+ request.getProtocol() + "<BR><BR>\n"
+ "<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Header Name<TH>Header Value");
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
Sting headerName
= (String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println("<TD>"
+ request.getHeader(headerName));
}
out.println("<TABLE>\n</BODY></HTML>");
}
}
HTTP 1.1 Request Headers
HTTP Response Codes
An HTTP server takes a request from a client and generates a response. Responses, like
requests, consist of a response line, headers, and a body. The response line contains the
HTTP version of the server, a response code, and a reason phrase. The reason phrase is
some text that describes the response, and could be anything, although a recommended set of
reason phrases is given in the specification. Response codes themselves are three-digit numbers
that are divided into groups. Each group has a meaning as shown here:
 1xx: Informational: Request received, continuing process.
 2xx: Success: The action was successfully received, understood, and accepted.
 3xx: Redirection: Further action must be taken in order to complete the request.
 4xx: User-Agent Error: The request contains bad syntax or cannot be fulfilled.
 5xx: Server Error: The server failed to fulfill an apparently valid request.
 Each Status: Code has an associated string (reason phrase).
 The status code you’ll see most often is 200. This means that every- thing has succeeded
and you have a valid response. The others you are likely to see are:
 401: you are not authorized to make this request
 404: cannot find the requested URI
 405:the HTTP method you have tried to execute is not sup- ported by this URL
(e.g., you have sent a POST and the URL will only accept GET)
 500: Internal Server Error. You are likely to see this if the resource to where you
are browsing (such as a Servlet) throws an exception.
Servlet – HTTP Status Codes
For each HTTP request and HTTP response, we have messages. The format of the HTTP request
and HTTP response messages are similar and will have the following structure −
 An initial status line + CRLF
 CRLF = ( Carriage Return + Line Feed i.e. New Line )
 Zero or more header lines + CRLF
 A blank line, i.e., a CRLF
 An optional message body like file, query data, or query output.
For example, a server response header looks like−

HTML
HTTP/5.0 200 OK
Content-Type: text/html
Headers:
(Blank Line)
<!doctype HTML>
<html>
<head></head>
<body>
</body>
</html>

The status line consists of the HTTP version (HTTP/5 in the example), a status code (200 in the
example), and a very short message corresponding to the status code (OK in the example). Here is
a list of HTTP status codes and associated messages that might be returned from the Web Server −

Status Code Message Description

Only a part of the request has been received by the server, but as
100 Continue long as it has not been rejected, the client should continue with the
request

Switching
101 The server switches protocol.
Protocols

200 OK The request is OK

201 Created The request is complete, and a new resource is created

The request is accepted for processing, but the processing is not


202 Accepted
complete.

203 Non-
Status Code Message Description

authoritative
Information

204 No content

205 Reset content

206 Partial content

Multiple A link list. The user can select a link and go to that location.
300
choices Maximum five addresses

Moved
301 The requested page has moved to a new URL
permanently

302 Found The requested page has moved temporarily to a new URL

303 See other The requested page can be found under a different URL

304 Not Modified

305 Use Proxy

This code was used in a previous version. It is no longer used, but the
306 Unused
code is reserved

Temporary
307 The requested page has moved temporarily to a new URL
redirect

400 Bad request The server did not understand the request

401 Unauthorized The requested page needs a username and a password

Payment
402 You cannot use this code yet
required

403 Forbidden Access is forbidden to the requested page

404 Not found The server cannot find the requested page.
Status Code Message Description

Method not
405 The method specified in the request is not allowed.
found

The server can only generate a response that is not accepted by the
406 Not Acceptable
client.

Proxy
You must authenticate with a proxy server before this request can be
407 Authentication
served.
Required

Request
408 The request took longer than the server was prepared to wait.
Timeout

409 Conflict The request could not be completed because of a conflict.

410 Gone The requested page is no longer available.

Length The “Content-Length” is not defined. The server will not accept the
411
Required request without it.

Precondition The precondition given in the request was evaluated as false by the
412
Failed server.

Request Entity The server will not accept the request, because the request entity is
413
Too Large too large.

The server will not accept the request, because the URL is too long.
Request-url
414 Occurs when you convert a “post” request to a “get” request with
Too Long
long query information.

Unsupported The server will not accept the request, because the media type is not
415
Media Type supported.

Expectation
417
Failed

Internal Server The request was not completed. The server met an unexpected
500
Error condition.

Not The request was not completed. The server did not support the
501
Implemented functionality required.
Status Code Message Description

The request was not completed. The server received an invalid


502 Bad Gateway
response from the upstream server.

Service The request was not completed. The server is temporarily


503
Unavailable overloading or down.

Gateway
504 The gateway has timed out.
Timeout

HTTP Version
505 The server does not support the “http protocol” version.
Not Supported

Methods to Set HTTP Status Code

The below methods can be used to set HTTP Status Code in your servlet program. These methods
are available with the HttpServletResponse object.

S.No
. Method and Description

public void setStatus ( int statusCode )


This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an
1 argument. If your response includes a special status code and a document, be sure to call setStatus
before actually returning any of the content with the PrintWriter.

public void sendRedirect(String url)


2 This method generates a 302 response along with a Location header giving the URL of the new
document

public void sendError(int code, String message)


3 This method sends a status code (usually 404) along with a short message that is automatically
formatted inside an HTML document and sent to the client.
HTTP Status Code Example
The code below is the example that would send a 407 error code to the client browser and the
browser would show you the “Not Found!!!” message.

Java
//Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// Extend HttpServlet class


public class showError extends HttpServlet {

// Method to handle GET method request.


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

// Set error code and reason.


response.sendError(404, "Not Found!!!" );
}

// Method to handle POST method request.


public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}
}

Now calling the above servlet would display the following result −

You might also like