In a Java EE project I have services that can throw errors such as throw new PersistenceException("My message here");
how do I get these error messages to display in a JSP view? I would think this is something simple to look up and figure out but I've had no success.
Asked
Active
Viewed 296 times
2

Hanna
- 10,315
- 11
- 56
- 89
2 Answers
2
There the <error-page>
declarations in web.xml
are for. E.g.
<error-page>
<exception-type>javax.persistence.PersistenceException<exception-type>
<location>/WEB-INF/errorpages/db.jsp</location>
</error-page>
Be however careful with declaring overly generic exception type (ServletException
or superclass) as "generic" error page, it would likely take over all exceptions. Use <error-code>500</error-code>
instead.
See also:
- Error Handler Servlet: how to get exception cause
- How does server prioritize which type of web.xml error page to use?
Note that I assume that those exceptions represent unrecoverable situations (i.e. there's no means of an user error which the enduser should fix by e.g. re-entering the input in proper format — just a simple validation error).
0
Catch the exception in the controller, put it (or its message) in a request attribute, and let the JSP get it from the request and display it, as it would with any other object.

JB Nizet
- 678,734
- 91
- 1,224
- 1,255