Servlets - Http Status Codes


The organization of the HTTP ask for and HTTP reaction messages are comparable and will have following structure −

  • An starting status line + CRLF ( Carriage Return + Line Feed for example New Line )

  • Zero or more header lines + CRLF

  • A clear line, i.e., a CRLF

  • An discretionary message body like document, question information or inquiry output.

For precedent, a server reaction header looks as pursues −

HTTP/1.1 200 OK 

Content-Type: content/html 

Header2: ... 

... 

HeaderN: ... 

(Clear Line) 

<!doctype ...> 

<html> 

<head>...</head> 

<body> 

... 

</body> 

</html> 

The status line comprises of the HTTP variant (HTTP/1.1 in the precedent), a status code (200 in the model), and an extremely short message comparing to the status code (OK in the example).

Following is a rundown of HTTP status codes and related messages that may be come back from the Web Server −

Code Message Description
100 Continue Only a piece of the demand has been gotten by the server, however as long as it has not been rejected, the customer should proceed with the request
101 Switching Protocols The server switches protocol.
200 OK The ask for is OK
201 Created The ask for is finished, and another asset is created
202 Accepted The ask for is acknowledged for preparing, yet the handling isn't complete.
203 Non-legitimate Information  
204 No Content  
205 Reset Content  
206 Partial Content  
300 Multiple Choices A connection list. The client can choose a connection and go to that area. Greatest five locations
301 Moved Permanently The asked for page has moved to another url
302 Found The asked for page has moved briefly to another url
303 See Other The asked for page can be found under an alternate url
304 Not Modified  
305 Use Proxy  
306 Unused This code was utilized in a past rendition. It is never again utilized, yet the code is reserved
307 Temporary Redirect The asked for page has moved incidentally to another url.
400 Bad Request The server did not comprehend the demand
401 Unauthorized The asked for page needs a username and a secret phrase
402 Payment Required You can't utilize this code yet
403 Forbidden Access is taboo to the asked for page
404 Not Found The server can't locate the asked for page.
405 Method Not Allowed The strategy indicated in the demand isn't allowed.
406 Not Acceptable The server can just produce a reaction that isn't acknowledged by the client.
407 Proxy Authentication Required You must validate with an intermediary server before this demand can be served.
408 Request Timeout The ask for took longer than the server was set up to wait.
409 Conflict The ask for couldn't be finished due to a conflict.
410 Gone The asked for page is no longer available.
411 Length Required The "Content-Length" isn't characterized. The server won't acknowledge the demand without it.
412 Precondition Failed The precondition given in the demand assessed to false by the server.
413 Request Entity Too Large The server won't acknowledge the demand, on the grounds that the demand element is too large.
414 Request-url Too Long The server won't acknowledge the demand, in light of the fact that the url is excessively long. Happens when you convert a "post" demand to a "get" ask for with a long question data.
415 Unsupported Media Type The server won't acknowledge the demand, in light of the fact that the media type isn't supported.
417 Expectation Failed  
500 Internal Server Error The ask for was not finished. The server met a surprising condition.
501 Not Implemented The ask for was not finished. The server did not bolster the usefulness required.
502 Bad Gateway The ask for was not finished. The server got an invalid reaction from the upstream server.
503 Service Unavailable The ask for was not finished. The server is briefly over-burdening or down.
504 Gateway Timeout The portal has planned out.
505 HTTP Version Not Supported The server does not bolster the "http convention" version.

Methods to Set HTTP Status Code

The following techniques can be utilized to set HTTP Status Code in your servlet program. These techniques are accessible with HttpServletResponse object.

Sr.No. Method & Description
1

public void setStatus ( int statusCode )

This technique sets a discretionary status code. The setStatus technique takes an int (the status code) as a contention. In the event that your reaction incorporates an exceptional status code and a record, make certain to call setStatus before really restoring any of the substance with the PrintWriter.

2

public void sendRedirect(String url)

This technique creates a 302 reaction alongside a Location header giving the URL of the new document

3

public void sendError(int code, String message)

This technique sends a status code (normally 404) alongside a short message that is consequently designed inside a HTML archive and sent to the client.

HTTP Status Code Example

Following is the precedent which would send a 407 blunder code to the customer program and program would demonstrate you "Need authentication!!!" message.

//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 deal with GET technique request. 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set mistake code and reason. 

response.sendError(407, "Need authentication!!!" ); 

} 

//Method to deal with POST technique request. 

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

doGet(request, response); 

} 

}

Now calling the above servlet would show the accompanying outcome −

HTTP Status 407 - Need authentication!!!

type Status report

messageNeed authentication!!!

descriptionThe customer should initially verify itself with the intermediary (Need authentication!!!).

Apache Tomcat/5.5.29







© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3