2

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.

Hanna
  • 10,315
  • 11
  • 56
  • 89

2 Answers2

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:

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).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
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