Monday, December 3, 2012

Error page Configuration in JSP

Error page configuration is no way related to exception handling because the jsp
equivalent servlet automatically takes care of exception handling .
Error pages are there to display non-technical error messages to the end users whenever
whenever exceptions are rised.


There are 2 way to configure error pages in jsp environment.

1) Local error page cfg
2) Global error cfg




Local error page cfg

************************


It is specific to only 1 jsp page.in this we use errorPage and isErrorPage attribute of <%@page---%>




Lets take an small instance:
-----------------------------------------





Deployment Directory Structure

--------------------------------------------


ErrJspLocal
             |-------WEB-INF
                            |----------------web.xml
            |--------Test.jsp
            |--------err.jsp



Test.jsp
----------

<%@page errorPage="err.jsp"%>
<b><i><center>Here is Test.jsp</b></i></center>
<% int x=Integer.parseInt("10abc");%>
<b>X value is   <%=x%> </b>


err.jsp
--------

<%@page isErrorPage="true"%>
<center><h1>From err.jsp</h1><br>
<font color=red size=5>Internal Problem
<br><br><br>

<b>Exception that is rised is: </b><%=exception.toString()%>
</font>
</center
>

web.xml
---------

<web-app/>

Here we are only dealing with jsp so web.xml is optional.....



Global Error page configuration in jap 

-----------------------------------------------------


If we have multiple jsp's and we want to configure an error page of all those jsp pages.
In this situation configuring seperate error page for each jsp is not recommended rather to
take only 1 jsp page as error page and configure it for multiple jsp's...
That is global error page configuration in jsp....
we have to configure this global error page in web.xml so that each jsp page can use this
error page.

Deployment Directory Structure

--------------------------------------------



ErrJspGlobal 

        |-------WEB-INF
                          |----------------web.xml
        |--------Test1.jsp
        |--------Test1.jsp
        |--------err.jsp


Test1.jsp
**********
<b><i><center>Here is Test1.jsp</b></i></center>
<% int x=Integer.parseInt("10a");%>
<b>X value is   <%=x%> </b>


Test2.jsp
***********
<b><i><center>Here is Test2.jsp</b></i></center>
<% Class c=Class.forName("java.lang.System");%>
<h2>Loaded Class is <%=c.getName()%></h2>




err.jsp
*******
<%@page isErrorPage="true"%>
<center><h1>From err.jsp</h1><br>
<font color=red size=5>Internal Problem
<br><br><br>

<b>Exception that is rised is: </b><%=exception.toString()%>
</font>
</center>


web.xml
*********
<web-app>
        <error-page>
                 <exception-type>java.lang.Exception</exception-type>
                 <location>/err.jsp</location>
        </error-page>
</web-app>

The only thing that we have to keep in mind while working with global error page is
Configure it into web.xml.
If you observe carefully there is no such special code in Test1 and Test2 and still we are
using only 1 error page for all the jsp's.


Thats how we can configure local and global error page in jsp ...

if we configure both ofcourse priority will be given to local error page ....

No comments:

Post a Comment