error-page in Web.xml , Handling error for both JSP & Servlet

Errors occur during the execution of a web application. Errors can be throwed by Servlet or JSP. When an error occurs , we can display the srror in a specifc error screen (Page) . That means we can map the error code returned by HTTP response or a Java Language exception returned by any web component to any page (Html or Jsp) .
This mapping is done by the element in Deployment Descriptor (Web.xml) . This is as an error page for both servlets and JSP pages. That is Global to the application. The following sample error-page element illustrates how to map a particular Error code or Exception with a specic Error Page.



<error-page>
<error-code>404</error-code>
<location>/filenotfound.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/badrequest.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.ClassNotFoundException</exception-type>
<location>/classnotfound.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.jsp.JspException</exception-type>
<location>/jspexcept.jsp</location>
</error-page>


If you need to use a different error page for some specific JSP pages (for example register.jsp) , you can use the page directive's errorPage attribute to override the web.xml declaration in those pages.

register.jsp
-------------

<%@ page language="java" contentType="text/html" errorPage="jspError.jsp" %>

if any error occurs in the register.jsp, then the jspError.jsp will be called instead of mapping error codes defined in Web.xml

0 comments:

Post a Comment

 
Copyright 2011 Java Online