Saturday, 28 June 2014

Servlets : HTTP Status Codes

🚀 Master Java Servlets, web architecture, and backend development!

🔔 Subscribe to Ram N Java on YouTube

What Are HTTP Status Codes?

Whenever a client (such as your web browser) sends a request to a web server or a Java Servlet, the server evaluates the request and sends back an HTTP response.

The first line of that response includes a 3-digit HTTP Status Code along with a reason phrase. This status code immediately tells the client whether the request succeeded, failed, needs redirection, or encountered an error on either the client or server side.

The 5 Standard Categories of HTTP Status Codes

All HTTP status codes are grouped into five distinct ranges based on the first digit:

  • 1xx (Informational): The request was received and the server is continuing the process (e.g., 100 Continue).
  • 2xx (Success): The action requested by the client was successfully received, understood, and accepted (e.g., 200 OK, 201 Created).
  • 3xx (Redirection): Further action needs to be taken by the client to fulfill the request (e.g., 301 Moved Permanently, 302 Found).
  • 4xx (Client Error): The request contains bad syntax or cannot be fulfilled by the server (e.g., 400 Bad Request, 404 Not Found).
  • 5xx (Server Error): The server failed to fulfill an apparently valid request due to an internal issue (e.g., 500 Internal Server Error, 503 Service Unavailable).

Setting Status Codes in Java Servlets

In Java Servlets, you can inspect and set status codes dynamically using the HttpServletResponse interface:

// Setting a standard success status
response.setStatus(HttpServletResponse.SC_OK);

// Sending an error status with a message
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource not found!");

Constants defined in HttpServletResponse make your Java code clean, self-descriptive, and compliant with standard HTTP specifications.

Related Tutorials You Might Enjoy


Click here to watch in Youtube : https://www.youtube.com/watch?v=P9_prhLXDbQ

Click the below Image to Enlarge
HTTP Status Codes
HTTP Status Codes
HTTP Status Codes
HTTP Status Codes
HTTP Status Codes
HTTP Status Codes

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials