What is the use of page object in JSP? Need an example.

JSPJava 8Object Oriented ProgrammingProgramming

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" %>

      <title>Error Handling Example</title>
         // Throw an exception to invoke the error page
         int x = 1;
         if (x == 1) {
            throw new RuntimeException("Error condition!!!");

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" %>

      <title>Show Error Page</title>
      <p>Sorry, an error occurred.</p>
      <p>Here is the exception stack trace: </p>
      <pre><% exception.printStackTrace(response.getWriter()); %></pre>

Access the main.jsp, you will receive an output somewhat like the following −

java.lang.RuntimeException: Error condition!!!

Sorry, an error occurred.

Here is the exception stack trace:
Updated on 30-Jul-2019 22:30:25