- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a common error page using JSP?
JSP gives you an option to specify Error Page for each JSP using page attribute. Whenever the page throws an exception, the JSP container automatically invokes the error page.
Following is an example to specifiy an error page for a main.jsp. To set up an error page, use the <%@ page errorPage = "xxx" %> directive.
<%@ page errorPage = "ShowError.jsp" %> <html> <head> <title>Error Handling Example</title> </head> <body> <% // Throw an exception to invoke the error page int x = 1; if (x == 1) { throw new RuntimeException("Error condition!!!"); } %> </body> </html>
We will now write one Error Handling JSP ShowError.jsp, which is given below. Notice that the error-handling page includes the directive <%@ page isErrorPage = "true" %>. This directive causes the JSP compiler to generate the exception instance variable.
<%@ page isErrorPage = "true" %> <html> <head> <title>Show Error Page</title> </head> <body> <h1>Opps...</h1> <p>Sorry, an error occurred.</p> <p>Here is the exception stack trace: </p> <pre><% exception.printStackTrace(response.getWriter()); %></pre> </body> </html>
Access the main.jsp, you will receive an output somewhat like the following −
java.lang.RuntimeException: Error condition!!! ...... Opps... Sorry, an error occurred. Here is the exception stack trace:
- Related Articles
- How to send a html based email using a JSP page?
- How to send a email with attachment using a JSP page?
- I want to use %> literal in JSP page. But it is throwing error. How to escape this syntax in JSP?
- How to send a simple text based email using a JSP page?
- How to send an error code using JSP to browser?
- How to handle error object in JSP using JSTL tags?
- How to write a comment in a JSP page?
- How to track access time of a webpage using session in a JSP page?
- How can I create custom tag in JSP which can accept attribute from parent jsp page?
- How to refresh a JSP page at regular interval?
- How to create a "Coming Soon" page using JavaScript?
- How to write a for loop in a JSP page?
- How to write a while loop in a JSP page?
- How to write a switch statement in a JSP page?
- How to avoid Java code in jsp page?

Advertisements