Exception Handling:
If we are configuring error pages in declarative approach that error page is applicable for entire application.
Test.jsp if any exception (or) error occurs then error.jsp is responsible to report this error.
Example: main.jsp
ShowError.jsp
We can configure error page in jsp’s by using the following two approaches:
1. Declarative Approach
2. Programmatic Approach
1. Declarative Approach:
We can configure error pages according to a particular exception (or) according to error code in web.xml as follows.
<web-app> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/generalError.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/exp404.html </location> </error-page> </web-app> |
If we are configuring error pages in declarative approach that error page is applicable for entire application.
2. Programmatic Approach
We can configure error page for a particular Jsp by using error page attribute of page Directive.
Test.jsp
<% @ page errorPage = “/Error.jsp” %> <h1> The Result is: <% = 10/0 %> |
Test.jsp if any exception (or) error occurs then error.jsp is responsible to report this error.
Programmatic Approach error page configuration is applicable only for a particular Jsp.
We can declare a Jsp as error page by using isErrorPage attribute of page directive.
In this error pages only exception implicit object is available.
Error.jsp
<%@ page isErrorPage = “true” %> <h1> can you plz provide valid input <br> The Exception is: <% = exception %> </h1> |
If the jsp is not declared as error page then exception implicit object is not available. if we are trying to use we will get compile time error saying Exception cannot be resolved.
If we are accessing error page directly without any exception hence exception implicit object refers null.
Which approach is recommended:
Declarative approach is always recommended because we can customize error pages based on exception type (or) error code. Such type of approach is called Declarative.
The following implicit objects are specially designed for Jsp’s.
Note : All Jsp implicit objects are available as local variables of _jspService(). So we can use this implicit objects within _jspService( ) but outside of _jspService() we can’t use , that means we can use inside scriptlet and expression but we can’t use inside declaration tag. |
Example: main.jsp
<%@ 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> |
ShowError.jsp
<%@ 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> |
Output:
java.lang.RuntimeException: Error condition!!! ...... Opps... Sorry, an error occurred. Here is the exception stack trace: |
No comments:
Post a Comment