Configuring error pages in the jsp
exception implicit object
We can configure error pages in jsp by using the following two approaches.
- Declarative approach
- Programmatic approach
We can configure error pages for a particular exception or for a particular error-code at web-application level as follows.
<web-app> <error-page> <exception-type>java.lang.ArithmeticException</exception-type> <location>/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <web-app>
The error configured in this approach are applicable for entire web-application.
We can configure error-pages for a particular jsp by using error page attribute of page directive.
<%@ page errorPage ="second.jsp"%> <% out.println(10/10); %>
In first jsp if any exception or error occurs then second.jsp is responsible to report this error. This way of configuring error page is applicable for a particular jsp. We can declare a jsp as error page by using is error page attribute of page directive.
<%@ page isErrorPage = "true"%> <h1> your provided i/p is not valid Can you plz provide valid i/p </h1> The raised exception is <% = exception%>
exception implicit object is available only in error pages i.e we have to use is error page = “true”.
If we are accessing error page directly without any exception then exception implicit object refers null.
- Among all jsp implicit objects except session and exception all the remaining objects are always available in every jsp we can’t make them unavailable. Session object is available by default in every jsp we can make it unavailable by using page directive.
- exception implicit object is not available by default in every jsp but we can make it available by declaring jsp as error page. We can declare it as follows.
- All these implicit objects as local variables of _jspservice() method. We are allowed to use within the scriplet & expression tag because the corresponding code will be placed within _jspservice() method . But we are not allowed to use jsp implicit objects inside declaration tag because it’s code be placed outside of –jspservlet() method.
<%@ page session = "false"%>
<%@ page is errorPage = "true"%>