Updated April 12, 2023
Introduction to JSP Error Page
In jsp technology code base we have created separate jsp files for web page errors in the jsp code base. To create an error page first we need to set and assign the page directive attribute in the initial stage of the jsp file we set it as is Error Page value is to be true when we need to assign the values like these type of attributes in the jsp page then we have a separate format to access the jsp exceptions using some implicit objects it will be more helpful for creating the customized error messages to the web application clients.
Syntax
When we create the jsp file as an error page we can import the page value as the basic format and syntax as below.
<%@ page errorPage="filename.jsp"%>
<%@ page isErrorPage="true"%>
<%@ taglib uri="" prefix=""%>
<html>
<body>
---some html program codes---
</body>
</html>
The above codes are the basic syntax in web application we have to set error page in two different ways one is assigned the values with the help of directives and another thing is we have to import the error page file in the jsp file.
How does JSP Error Page work?
If we want to use the jsp page as an error page we can declare more exceptions will arise when the jsp page is to be executed. When the jsp page code is executed it will automatically call and moves to the jsp web container it should be accessed and forward the controls to the page if the particular jsp application is going to throw an exception if the exception occurs it should be thrown to the including the page directive as the beginning of the jsp page.
Whenever the directive is assigned to the page the directive makes and creates an instance of the type like “javax.servlet.jsp.ErrorData” is availabled to the so that the container will retrieve the messages, interpret the data, and also possible to display the error data information about the root cause of the errors and exceptions in the particular jsp application. We can also access the error data jsp pages using EL(Expression Language) expressions is one of the ways of the error context in the application.
Using ${pageContext.errorData.statusCode} we can retrieve the page status code and also using ${pageContext.errorData.throwable} we can retrieve the errors and throws of the errors and exceptions in the web page. We can retrieve and helps to identify the cause of the exceptions in the log files using the expression like ${pageContext.errorData.throwable.cause}.The jsp web page has the option to throw the specific errors in the page for each type of jsps.
When the jsp page throws the exceptions every time the jsp containers automatically invoke the error pages is one of the main advantages. This contains sometimes types of exceptions that will be thrown to the particular instances using the exception instance based on its parent class called Throwable class is assigned and declared the types of exceptions in the jsp pages. Basically errors are most probably ignored by the codes because we can do some things occasionally or rarely in the pages.
Errors are not generally like Exceptions basically whenever the code side programmer gets trouble situations like out of control that time it throws something like error status code, messages, etc. In programmer code logic like the memory of the arrays exceeds the beyond limits that time the out of memory error occurs, a stack overflow occurs like these types of things will be out of the scopes in programmers it also ignored in the compile-time situations.
Checked exceptions and Un-checked Exceptions(Runtime Exceptions) is a general category of the exceptions if suppose file not found exceptions we know that it is basically kind of exceptions but most probably it is like the user error the particular user, unfortunately, open the file or they do some things in the file that time its throws exceptions then runtime exceptions are ignored by the coders at the compilation time.
Examples to Implement JSP Error Page
Below are examples mentioned:
Example #1
Code:
<%@ page errorPage = "first.jsp" %>
<html>
<head>
<title>Error Handling Example</title>
</head>
<body>
<%
int i = 1;
if (i == 1) {
throw new RuntimeException("Error occcured!!!");
}
%>
</body>
</html>
<%@ page isErrorPage = "true" %>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<h1>Error</h1>
<p>The error is occured.</p>
<p>Below are the exceptions occured and its traced: </p>
<pre><% exception.printStackTrace(response.getWriter()); %></pre>
</body>
</html>
Output:
Example #2
Code:
<html>
<head>
<title>Second Example</title>
</head>
<body>
<%
try {
int x = 1;
x = x / 0;
out.println("Result is " + x);
}
catch (Exception e) {
out.println("The exception occurred: " + e.getMessage());
}
%>
</body>
</html>
Output:
Example #3
Code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<title>Second Page</title>
</head>
<body>
<h1>Error Page Occurs</h1>
<div style="color: 'green';">
The given Error message is: <%= exception.toString() %>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page errorPage="https://cdn.educba.com/second.jsp" %>
<html>
<head>
<title>First Page</title>
</head>
<body>
<h1>Error Page Occurs</h1>
<%
int n = Integer.parseInt("Welcome To My Domain!");
%>
</body>
</html>
Output:
Explanation: In the above three examples we used different scenarios for the jsp in web applications. The first example we used the runtime exceptions in the two different jsp files but both the files error value is set and used for catch and throw the exceptions, the second example we used numbers using mathematical expressions like infinity values will have occurred and it has to be caught and displayed it in the web page the final example we use again two different files we used css styles presentation error is to occur and we get the output has stack trace in the log files.
Conclusion
In web pages we have created and customize the web applications based on the user needs and also it may vary upon the user situations or else application update failed time error occurs on the user browser page.
Recommended Articles
This is a guide to JSP Error Page. Here we discuss an introduction to JSP Error Page with appropriate syntax and respective programming examples. You can also go through our other related articles to learn more –