Exception Handling in JSP

Last updated on May 31 2022
Mohnish Patil

Table of Contents

Exception Handling in JSP

In this blog, we will discuss how to handle exceptions in JSP. When you are writing a JSP code, you might make coding errors which can occur at any part of the code. There may occur the following type of errors in your JSP code −

Checked exceptions

A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

Runtime exceptions

A runtime exception is an exception that probably could have been avoided by the programmer. As opposed to the checked exceptions, runtime exceptions are ignored at the time of compliation.

Errors

These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

We will further discuss ways to handle run time exception/error occuring in your JSP code.

Using Exception Object

The exception object is an instance of a subclass of Throwable (e.g., java.lang. NullPointerException) and is only available in error pages. Following table lists out the important methods available in the Throwable class.

S.No. Methods & Description
1 public String getMessage()

Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.

2 public Throwable getCause()

Returns the cause of the exception as represented by a Throwable object.

3 public String toString()

Returns the name of the class concatenated with the result of getMessage().

4 public void printStackTrace()

Prints the result of toString() along with the stack trace to System.err, the error output stream.

5 public StackTraceElement [] getStackTrace()

Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.

6 public Throwable fillInStackTrace()

Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.

JSP gives you an option to specify Error Page for each JSP. 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:

Using JSTL Tags for Error Page

You can make use of JSTL tags to write an error page ShowError.jsp. This page has almost same logic as in the above example, with better structure and more information −

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<%@page isErrorPage = "true" %>


<html>

   <head>

      <title>Show Error Page</title>

   </head>

  

   <body>

      <h1>Opps...</h1>

      <table width = "100%" border = "1">

         <tr valign = "top">

            <td width = "40%"><b>Error:</b></td>

            <td>${pageContext.exception}</td>

         </tr>

           

         <tr valign = "top">

            <td><b>URI:</b></td>

            <td>${pageContext.errorData.requestURI}</td>

         </tr>

           

         <tr valign = "top">

            <td><b>Status code:</b></td>

            <td>${pageContext.errorData.statusCode}</td>

         </tr>

           

         <tr valign = "top">

            <td><b>Stack trace:</b></td>

            <td>

               <c:forEach var = "trace"

                  items = "${pageContext.exception.stackTrace}">

                  <p>${trace}</p>

               </c:forEach>

            </td>

         </tr>

      </table>


   </body>

</html>

Access the main.jsp, the following will be generated −

 

Error: java.lang.RuntimeException: Error condition!!!
URI: /main.jsp
Status code: 500
Stack trace: org.apache.jsp.main_jsp._jspService(main_jsp.java:65)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)

javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

 

Using Try…Catch Block

If you want to handle errors within the same page and want to take some action instead of firing an error page, you can make use of the try….catch block.

Following is a simple example which shows how to use the try…catch block. Let us put the following code in main.jsp −

<html>

   <head>

      <title>Try...Catch Example</title>

   </head>

  

   <body>

      <%

         try {

            int i = 1;

            i = i / 0;

            out.println("The answer is " + i);

         }

         catch (Exception e) {

            out.println("An exception occurred: " + e.getMessage());

         }

      %>

   </body>

</html>

Access the main.jsp, it should generate an output somewhat like the following –

An exception occurred: / by zero

So, this brings us to the end of blog. This Tecklearn ‘Exception Handling in JSP’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn JSP and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  • GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Exception Handling in JSP"

Leave a Message

Your email address will not be published. Required fields are marked *