JSP
Web.xml Filter Mapping in JSP Servlet with Example
What is JSP Filter? Filters are used for filtering functionality of the Java web application. They...
Exceptions occur when there is an error in the code either by the developer or internal error from the system.
Exception handling in JSP is same as in java where we manage exceptions using try catch blocks.
Unlike Java, there are exceptions in JSP also when there is an error in the code.
Exceptions are of three types:
It is normally a user error or problems which are not seen by the developer are termed as checked exceptions.
Some of the examples are:
Runtime exceptions are the one which could have avoided by the programmer. They are ignored at the time of compilation.
Some of the examples are:
Errors:
The problem arises due to the control of the user or programmer. If stack overflows, then error can occur.
Some examples of the error are listed below:
It is an instance of the throwable class, and it is used in error pages.
Some methods of throwable class are:
Example
Exception_example.jsp
<%@ page errorPage="guru_error.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>Exception Guru JSP1</title>
</head>
<body>
<%
int num = 10;
if (num == 10)
{
throw new RuntimeException("Error condition!!!");
}
%>
</body>
</html>
Guru_error.jsp
<%@ page isErrorPage="true" %>
<%@ 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>Guru Exception Page</title>
</head>
<body>
<p>Guru Exception has occurred</p>
<% exception.printStackTrace(response.getWriter()); %>
</body>
</html>
Explanation of the code:
Exception_example.jsp
Code Line 1: Here we are setting error page to guru_error.jsp which will be used when the error will be redirected.
Code Line 15: we are taking a variable num and setting it to 10 and checking a condition if num is 10 then to throw a Runtime Exception with the message as Error Condition.
Guru_error.jsp
Code Line 1: Here we are setting isErrorPageattribute to true.
Code Line 12: The exception has been raised in exception_example.jsp using throw object and that exception will be shown here as IsErrorPage attribute is marked as true. Using the exception (this is an object which allows the exception data to be accessed by the JSP.) object we are trying to print the stacktrace of the error which was occurred in exception_example.jsp.
When you execute the above code you get the following output:
Output:
The exception has been raised which was thrown from exception_example.jsp using throw object of runtime exception and we get the above code.
Also guru_error.jsp is called from which Guru Exception has occurred from this file.
Summary:
Here we have learnt about exceptions in this tutorial and taken an example of runtime exception in the tutorial.
What is JSP Filter? Filters are used for filtering functionality of the Java web application. They...
In this tutorial, we are going develop sample programs with JSP and using MVC architecture....
Download PDF Following Spring interview questions are for freshers and experienced users 1) What is...
What is JSP? Java Server Pages (JSP) is a technology which is used to develop web pages by...
In this example, we are going to learn about uploading and downloading of a file through JSP. File...
JSP Date Handling All methods of core Java can be used in JSP is the biggest advantage of JSP. In...