In this tutorial you will learn about how to send error as a response.
JSP Response sendError
In this tutorial you will learn about how to send error as a response.
To send error as response to the client a method sendError() is used. This method is of HttpServletResponse interface that sends error using the specified status code.
Syntax :
void sendError(int sc) throws java.io.IOException
Argument of this method is a Status code. And it throws IOExcpetion if the response has already committed.
Example :
In the example given below I have created a JSP page where I have designed a form to take input by the user. Then set the buffer size to 8 kb and checked for the value whether it is null, if it returns true then the error page will be displayed.
jspRespondSendError.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Response sendError</title>
</head>
<body>
<form>
Enter content <input type="text" name="content"/>
<input type="submit" value="submit"/>
</form>
<%
response.setBufferSize(8 * 1024);
int size = response.getBufferSize();
if (request.getParameter("content") == null) {
response.sendError(response.SC_BAD_REQUEST, "content needed");
}
%>
</body>
</html>
Output :
When you will execute the above jsp page an output will be as follows :


[ 0 ] Comments