HTTP Response Codes
HTTP Response Codes
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 −
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
203 Non-
Status Code Message Description
authoritative
Information
204 No 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
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
Payment
402 You cannot use this code yet
required
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
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
Gateway
504 The gateway has timed out.
Timeout
HTTP Version
505 The server does not support the “http protocol” version.
Not Supported
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
Java
//Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
doGet(request, response);
}
}
Now calling the above servlet would display the following result −