How can I handle exceptions and errors in a Java web application?

Koray Tugay

How can I make sure user does not see any ugly stack trace and redirect the user to some appropriate page based on an exception or a server fault such as 404 not found in a Java web application that uses jsp?

Koray Tugay

There are few ways to achieve this.

In the web.xml you can have something like this for example:

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

So whenever the server can not find the requested resource, the user will be redirected to the given page. You can also handle Exceptions like this, again in the web.xml

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

And you can handle all exceptions by:

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

Also, in the jsp file itself you can have a JSP directive like this, that will override anything in web.xml:

<%@ page errorPage="errorpage.jsp" %>

This means if anything goes wrong in the jsp, errorpage.jsp will be loaded. errorpage.jsp will also need a directive in it as follows, so that pageWithError.jsp can actually be forwarded to errorpage.jsp:

<%@ page isErrorPage="true" %>

So if you have the directive in the jsps(both the page that has the error, and the page that will be shown in case of an error), that will be taken into account. If not, and some error happens, web.xml will be consulted. Still no matches? Well then a 404 or a stacktrace will be shown..

Directive in the jsp is like overriding web.xml.

Hope it helps!!!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to handle all exceptions in a web java project

How can I handle network exceptions in VBA?

How can I handle errors in picocli?

How can I handle _popen() errors in C?

How can I handle input errors in HotChocolate?

how can I handle invalid data rows using custom exceptions in Java?

How to handle exceptions when writing a library (not an application) - Java

How to handle Errors and Exceptions in Redux Reducers?

How can I get errors and exceptions ,and write them to log files?

How can I catch errors and exceptions in Symfony2/Silex?

How can I handle exceptions that Process.run throws

How can I handle FileNotFoundError exceptions for Kivy FileChooserController

How can I handle exceptions with Spring Data Rest and the PagingAndSortingRepository?

How can I handle exceptions in WebAPI 2. methods?

How can I handle exceptions caused in Elmah itself?

how can i handle value error exceptions many times in python

How do I handle exceptions?

How do I handle multiple checked exceptions in Java?

How can I speedup Java web application compilation?

How can I Make Single Application for web and desktop in JAVA

How can I pass only Exceptions from my Java Library to the implementing application?

How do I handle errors without checked exceptions in a services orchestration scenario?

How can I handle runtime errors on Express app?

RACSignal and replayLazily. How can I handle errors?

How can I handle errors when copying file over Network

how can I get handle errors from UseInterceptors Decorator

how can i handle errors and continue function ? Javascript js

How to handle exceptions from webjobs in application insights?

How can I handle branching calculations in Java?

TOP Ranking

HotTag

Archive