GeeCON Prague 2014*
The moose likes Servlets and the fly likes Overriding <exception-type> tag in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Overriding <exception-type> tag in web.xml" Watch "Overriding <exception-type> tag in web.xml" New topic
Author

Overriding <exception-type> tag in web.xml

Sanket Apte
Greenhorn

Joined: Apr 30, 2009
Posts: 3
Consider the following code:

try
{ ... }
catch ( IOException ie )
{ ... }
catch ( Exception e )
{ ... }

Here, in case of IOException, the first 'catch' block will execute whereas in case of any other exception, the second 'catch' block will execute. Can we do the same thing in 'web.xml' ( Deployment Descriptor for any servlet )?? Suppose, I want to redirect the user to one.jsp if ArithmeticException occurs and redirect the user to two.jsp if any other exception occurs, how would I configure my 'web.xml'?

This is what I tried which is not working out:

<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/one.jsp</location>
</error-page>

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/two.jsp</location>
</error-page>

Any help is greatly appreciated... Thanks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sanket Apte wrote:
This is what I tried which is not working out:


Working for me. and Welcome to javaranch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and i guess, you tried in IE browser if yes. tools---->internetoption-->advance-->uncheck the Show http friendly messages
Sanket Apte
Greenhorn

Joined: Apr 30, 2009
Posts: 3
Thanks a lot for the answers. But, it is still not working for me. I tried in IE as well as in Google Chrome. This is my code....

web.xml
================================================================
<?xml.... >

<servlet> ... </servlet>

<servlet-mapping> ... </servlet-mapping>

<error-page>
<error-code>404</error-code>
<location>/PageNotFoundErrors.jsp</location>
</error-page>

<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/ArithmeticErrors.jsp</location>
</error-page>

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/OtherErrors.jsp</location>
</error-page>

</web-app>

jsp page:
====================================================================================
<body>

<%!
public void recursion() // This function calls itself countless times and finally produces a StackOverflowError
{
recursion();
}
%>

Enter 1 for ArithmeticException, 2 for 'Page not found', 3 for 'Other errors'... <br />
<form method = "POST">
<input type = "text" name = "option" />
<br /><br />
<input type = "submit" />
</form>

<c:if test = "${pageContext.request.method == 'POST'}">
<c:choose>
<c:when test = "${param.option == '1'}" >
<% int i = 10%0; // This code generates an ArithmeticException %>
</c:when>

<c:when test = "${param.option == '2'}" >
<c:redirect url = "/NonExistentPage.jsp" />
</c:when>

<c:when test = "${param.option == '3'}">
<% recursion(); %>
</c:when>

<c:otherwise>
<c:out value = "There are no errors..." />
</c:otherwise>
</c:choose>
</c:if>

</body>

=================================================================================

Everytime I run this, no matter what error is generated, I am always redirected to OtherErrors.jsp
What can be the solution?
mike ickx
Greenhorn

Joined: Apr 30, 2009
Posts: 9
think you find the solution here http://docs.codehaus.org/display/JETTY/How+to+Create+Custom+Error+Pages
context file configuration.


Sanket Apte
Greenhorn

Joined: Apr 30, 2009
Posts: 3
@ Mike,

Thanks a lot for help, but the link you have provided is for Jetty server. I use Tomcat 6.x. How do I configure my web.xml for Tomcat?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding <exception-type> tag in web.xml