SlideShare a Scribd company logo
SERVLETS - HTTP STATUS CODES 
http://www.tuto rialspo int.co m/se rvle ts/se rvle ts-http-status-co de s.htm Co pyrig ht © tuto rials po int.c om 
T he format of the HT T P request and HT T P response messag es are similar and will have following structure: 
An initial status line + CRLF ( Carriag e Return + Line Feed ie. New Line ) 
Z ero or more header lines + CRLF 
A blank line ie. a CRLF 
An optioanl messag e body like file, query data or query output. 
For example, a server response header looks as follows: 
HTTP/1.1 200 OK 
Content-Type: text/html 
Header2: ... 
... 
HeaderN: ... 
(Blank Line) 
<!doctype ...> 
<html> 
<head>...</head> 
<body> 
... 
</body> 
</html> 
T he status line consists of the HT T P version (HT T P/1.1 in the example), a status code (200 in the example), and 
a very short messag e corresponding to the status code (OK in the example). 
Following is a list of HT T P status codes and associated messag es that mig ht be returned from the Web Server: 
Code: Messag e: Desc ription: 
100 Continue Only a part of the request has been received by the server, but 
as long as it has not been rejected, the client should continue 
with the request 
101 Switching Protocols T he server switches protocol. 
200 OK T he request is OK 
201 Created T he request is complete, and a new resource is created 
202 Accepted T he request is accepted for processing , but the processing is 
not complete. 
203 Non-authoritative Information 
204 No Content 
205 Reset Content 
206 Partial Content 
300 Multiple Choices A link list. T he user can select a link and g o to that location. 
Maximum five addresses 
301 Moved Permanently T he requested pag e has moved to a new url 
302 Found T he requested pag e has moved temporarily to a new url
302 Found T he requested pag e has moved temporarily to a new url 
303 See Other T he requested pag e can be found under a different url 
304 Not Modified 
305 Use Proxy 
306 Unused T his code was used in a previous version. It is no long er used, 
but the code is reserved. 
307 T emporary Redirect T he requested pag e has moved temporarily to a new url. 
400 Bad Request T he server did not understand the request 
401 Unauthorized T he requested pag e needs a username and a password 
402 Payment Required You can not use this code yet 
403 Forbidden Access is forbidden to the requested pag e 
404 Not Found T he server can not find the requested pag e. 
405 Method Not Allowed T he method specified in the request is not allowed. 
406 Not Acceptable T he server can only g enerate a response that is not accepted 
by the client. 
407 Proxy Authentication Required You must authenticate with a proxy server before this request 
can be served. 
408 Request T imeout T he request took long er than the server was prepared to wait. 
409 Conflict T he request could not be completed because of a conflict. 
410 Gone T he requested pag e is no long er available. 
411 Leng th Required T he "Content-Leng th" is not defined. T he server will not accept 
the request without it. 
412 Precondition Failed T he precondition g iven in the request evaluated to false by the 
server. 
413 Request Entity T oo Larg e T he server will not accept the request, because the request 
entity is too larg e. 
414 Request-url T oo Long T he server will not accept the request, because the url is too 
long . Occurs when you convert a "post" request to a "g et" 
request with a long query information. 
415 Unsupported Media T ype T he server will not accept the request, because the media type 
is not supported. 
417 Expectation Failed 
500 Internal Server Error T he request was not completed. T he server met an unexpected 
condition 
501 Not Implemented T he request was not completed. T he server did not support the 
functionality required. 
502 Bad Gateway T he request was not completed. T he server received an invalid 
response from the upstream server 
503 Service Unavailable T he request was not completed. T he server is temporarily 
overloading or down.
504 Gateway T imeout T he g ateway has timed out. 
505 HT T P Version Not Supported T he server does not support the "http protocol" version. 
Methods to Set HTTP Status Code: 
T here are following methods which can be used to set HT T P Status Code in your servlet prog ram. T hese 
methods are available with HttpServletResponse object. 
S.N. Method & Desc ription 
1 public void setStatus ( int statusCode ) 
T his method sets an arbitrary status code. T he setStatus method takes an int (the status code) as an 
arg ument. 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. 
2 public void sendRedirec t(String url) 
T his method g enerates a 302 response along with a Location header g iving the URL of the new 
document. 
3 public void sendError(int c ode, String messag e) 
T his method sends a status code (usually 404) along with a short messag e that is automatically 
formatted inside an HTML document and sent to the client. 
HTTP Status Code Example: 
Following is the example which would send 407 error code to the client browser and browser would show you 
"Need authentication!!!" messag e. 
// 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(407, "Need authentication!!!" ); 
} 
// 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 following result: 
HTTP STATUS 407 - NEED AUTHENTICATION!!! 
t ype Status report
message Need authentication!!! 
desc ript ion T he client must first authenticate itself with the proxy (Need authentication!!!). 
Apache Tomcat /5.5.29

More Related Content

PPTX
Http Status Code Errors in SEO
PDF
HTTP Status Codes Cheat Sheet: An Exhaustive List
DOCX
SEO HTTP Response Codes List
PPTX
IMPM Presentation 2010
PPTX
遠在德國的賴芬蘭稍來廣慈開發環評書面意見
PPTX
Los dispositivos móviles en educación
PPTX
Another aristotle presentation
PDF
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Http Status Code Errors in SEO
HTTP Status Codes Cheat Sheet: An Exhaustive List
SEO HTTP Response Codes List
IMPM Presentation 2010
遠在德國的賴芬蘭稍來廣慈開發環評書面意見
Los dispositivos móviles en educación
Another aristotle presentation
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?

Similar to Servlets http-status-codes (20)

PPTX
Web technology Unit-I Part D - message format
PPTX
HTTP Request Header and HTTP Status Code
PPTX
BITM3730Week9(1).pptx
PDF
BITM3730 Networking.pdf
PPTX
BITM3730 11-1.pptx
PPTX
The Top Tips You need to Learn about Data in your Mobile App
PPTX
Http Status Message - Pocket Guide
PDF
REST 101: An Overview To Representational State Transfer.
PDF
HTTP Request and Response Structure
PPTX
Web Application Technologies
PPTX
Module 5.pptx HTTP protocol on optical and wireless communication
PPT
Http request&response session 1 - by Vignesh.N
PPTX
HTTP Status Codes you should know and use while building APIs
PPT
Http request&response by Vignesh 15 MAR 2014
PPTX
Http status code 416 vs 428, 503 vs 505
PDF
A Simple Guide to Proxy Error and Troubleshooting Issues
PDF
Great webapis
PPTX
Troubleshooting.pptx
PPTX
Web technology Unit-I Part D - message format
HTTP Request Header and HTTP Status Code
BITM3730Week9(1).pptx
BITM3730 Networking.pdf
BITM3730 11-1.pptx
The Top Tips You need to Learn about Data in your Mobile App
Http Status Message - Pocket Guide
REST 101: An Overview To Representational State Transfer.
HTTP Request and Response Structure
Web Application Technologies
Module 5.pptx HTTP protocol on optical and wireless communication
Http request&response session 1 - by Vignesh.N
HTTP Status Codes you should know and use while building APIs
Http request&response by Vignesh 15 MAR 2014
Http status code 416 vs 428, 503 vs 505
A Simple Guide to Proxy Error and Troubleshooting Issues
Great webapis
Troubleshooting.pptx
Ad

Recently uploaded (20)

PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
DOCX
Unit-3 cyber security network security of internet system
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
Internet___Basics___Styled_ presentation
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Digital Literacy And Online Safety on internet
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
Sims 4 Historia para lo sims 4 para jugar
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
QR Codes Qr codecodecodecodecocodedecodecode
SAP Ariba Sourcing PPT for learning material
RPKI Status Update, presented by Makito Lay at IDNOG 10
Unit-3 cyber security network security of internet system
presentation_pfe-universite-molay-seltan.pptx
Internet___Basics___Styled_ presentation
WebRTC in SignalWire - troubleshooting media negotiation
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Digital Literacy And Online Safety on internet
Design_with_Watersergyerge45hrbgre4top (1).ppt
international classification of diseases ICD-10 review PPT.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Sims 4 Historia para lo sims 4 para jugar
Tenda Login Guide: Access Your Router in 5 Easy Steps
Decoding a Decade: 10 Years of Applied CTI Discipline
Cloud-Scale Log Monitoring _ Datadog.pdf
Ad

Servlets http-status-codes

  • 1. SERVLETS - HTTP STATUS CODES http://www.tuto rialspo int.co m/se rvle ts/se rvle ts-http-status-co de s.htm Co pyrig ht © tuto rials po int.c om T he format of the HT T P request and HT T P response messag es are similar and will have following structure: An initial status line + CRLF ( Carriag e Return + Line Feed ie. New Line ) Z ero or more header lines + CRLF A blank line ie. a CRLF An optioanl messag e body like file, query data or query output. For example, a server response header looks as follows: HTTP/1.1 200 OK Content-Type: text/html Header2: ... ... HeaderN: ... (Blank Line) <!doctype ...> <html> <head>...</head> <body> ... </body> </html> T he status line consists of the HT T P version (HT T P/1.1 in the example), a status code (200 in the example), and a very short messag e corresponding to the status code (OK in the example). Following is a list of HT T P status codes and associated messag es that mig ht be returned from the Web Server: Code: Messag e: Desc ription: 100 Continue Only a part of the request has been received by the server, but as long as it has not been rejected, the client should continue with the request 101 Switching Protocols T he server switches protocol. 200 OK T he request is OK 201 Created T he request is complete, and a new resource is created 202 Accepted T he request is accepted for processing , but the processing is not complete. 203 Non-authoritative Information 204 No Content 205 Reset Content 206 Partial Content 300 Multiple Choices A link list. T he user can select a link and g o to that location. Maximum five addresses 301 Moved Permanently T he requested pag e has moved to a new url 302 Found T he requested pag e has moved temporarily to a new url
  • 2. 302 Found T he requested pag e has moved temporarily to a new url 303 See Other T he requested pag e can be found under a different url 304 Not Modified 305 Use Proxy 306 Unused T his code was used in a previous version. It is no long er used, but the code is reserved. 307 T emporary Redirect T he requested pag e has moved temporarily to a new url. 400 Bad Request T he server did not understand the request 401 Unauthorized T he requested pag e needs a username and a password 402 Payment Required You can not use this code yet 403 Forbidden Access is forbidden to the requested pag e 404 Not Found T he server can not find the requested pag e. 405 Method Not Allowed T he method specified in the request is not allowed. 406 Not Acceptable T he server can only g enerate a response that is not accepted by the client. 407 Proxy Authentication Required You must authenticate with a proxy server before this request can be served. 408 Request T imeout T he request took long er than the server was prepared to wait. 409 Conflict T he request could not be completed because of a conflict. 410 Gone T he requested pag e is no long er available. 411 Leng th Required T he "Content-Leng th" is not defined. T he server will not accept the request without it. 412 Precondition Failed T he precondition g iven in the request evaluated to false by the server. 413 Request Entity T oo Larg e T he server will not accept the request, because the request entity is too larg e. 414 Request-url T oo Long T he server will not accept the request, because the url is too long . Occurs when you convert a "post" request to a "g et" request with a long query information. 415 Unsupported Media T ype T he server will not accept the request, because the media type is not supported. 417 Expectation Failed 500 Internal Server Error T he request was not completed. T he server met an unexpected condition 501 Not Implemented T he request was not completed. T he server did not support the functionality required. 502 Bad Gateway T he request was not completed. T he server received an invalid response from the upstream server 503 Service Unavailable T he request was not completed. T he server is temporarily overloading or down.
  • 3. 504 Gateway T imeout T he g ateway has timed out. 505 HT T P Version Not Supported T he server does not support the "http protocol" version. Methods to Set HTTP Status Code: T here are following methods which can be used to set HT T P Status Code in your servlet prog ram. T hese methods are available with HttpServletResponse object. S.N. Method & Desc ription 1 public void setStatus ( int statusCode ) T his method sets an arbitrary status code. T he setStatus method takes an int (the status code) as an arg ument. 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. 2 public void sendRedirec t(String url) T his method g enerates a 302 response along with a Location header g iving the URL of the new document. 3 public void sendError(int c ode, String messag e) T his method sends a status code (usually 404) along with a short messag e that is automatically formatted inside an HTML document and sent to the client. HTTP Status Code Example: Following is the example which would send 407 error code to the client browser and browser would show you "Need authentication!!!" messag e. // 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(407, "Need authentication!!!" ); } // 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 following result: HTTP STATUS 407 - NEED AUTHENTICATION!!! t ype Status report
  • 4. message Need authentication!!! desc ript ion T he client must first authenticate itself with the proxy (Need authentication!!!). Apache Tomcat /5.5.29