A Java Servlet is a program that runs on a Web server or application server as an intermediary layer between requests from a Web browser or other HTTP client and a database or application on the HTTP server.
Using Servlets, you can collect user input from web forms, render records from a database or other sources, and dynamically create web pages.
Servlet HTTP status code syntax
The format of HTTP request and HTTP response messages is similar, and the structure is as follows:
Initial status line + carriage return and line feed (carriage return + line feed)
Zero or more A title line + carriage return and line feed character
A blank line, that is, carriage return and line feed character
An optional message body, such as a file, query data or query output
Servlet HTTP status code example
//Import the necessary java libraries import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;@WebServlet("/showError")//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); }}