There are following methods which can be used to set HTTP Status Code in your servlet program. These method are available with HttpServletResponse object.

S.N.

Method & Description

1

public void setStatus ( int statusCode )
This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. 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 sendRedirect(String url)
This method generates a 302 response along with a Location header giving the URL of the new document.

3

public void sendError(int code, String message)
This method sends a status code (usually 404) along with a short message that is automatically formatted inside an HTML document and sent to the client.