Pr.Pg Next Pg

Methods to Set HTTP Status Code in jsp tutorials

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

 

S.N.

Method

Description

1

public void setStatus(int statusCode)

 

Sets the status code for this response. This method is used to set the return status code when there is no error.

2

public void sendRedirect(String url)

 

Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs the servlet container must convert the relative URL to an absolute URL before sending the response to the client.

3

public void sendError(int code,
String message)

Sends an error response to the client using the specified status code and clearing the buffer.

 


 

JSP program to show status code 407 error

Sam Sir

<%-- StatusCodeError.jsp --%>

<%-- JSP program to show status code 407 error --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

response.sendError(407,"Proxy Authentication Required..");

%>

</body>

</html>

Output

Status Code

 


 

JSP Program to show status code 404 error

Sam Sir

<%-- StatusCodeError404.jsp --%>

<%-- JSP Program to show status code 404 error --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

response.sendError(404,"File Not Found...");

%>

</body>

</html>

Output

Status code

Pr.Pg border                                              Next Pg