This J2EE tips demonstrates a java servlet that returns an HTTP error code as page response in case an error has occured. For example, 5xx-series messages indicate that something went wrong on the server. Usually associated with CGI problems.

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class SendError extends HttpServlet {
  public void doGet(HttpServletRequest request , 
    HttpServletResponse response
    throws ServletException , IOException {
    
    response.setContentType("text/plain");
    
    PrintWriter out = response.getWriter();
    int a = request.getContentLength();
    
    if(a==-1) {
    // The Default content Length is -1
    
    response.sendError(response.SC_LENGTH_REQUIRED,
      "ContentLength Required");
    // Developer can use 411 in place of SC_LENGTH_REQUIRED
    }
  }

  public void doPost(HttpServletRequest request , 
    HttpServletResponse response
    throws ServletException , IOException {
    doGet(request , response);
    //Calling doGet from doPost() 
  }
}