Java Reference
In-Depth Information
Figure 13.1: A typical generic exception page
Here's how we can implement a simple exception handler that logs the
exception and forwards to a nicer page:
package
stripesbook.ext;
public class
MyExceptionHandler
implements
ExceptionHandler {
private static final
String VIEW = "/WEB-INF/jsp/exception.jsp";
private static final
Log log =
Log.getInstance(MyExceptionHandler.
class
);
public void
handle(Throwable exc, HttpServletRequest req,
HttpServletResponse resp)
throws
ServletException, IOException
{
log.error(exc);
req.getRequestDispatcher(VIEW).forward(req, resp);
}
public void
init(Configuration configuration) { }
}
MyExceptionHandler
will be loaded by Stripes because it is in an exten-
sion package that's configured in
web.xml
.
ExceptionHandler
is one of the
many extension interfaces that inherits from
ConfigurableComponent
,
an interface that defines the
init(Configuration)
method. Stripes calls this
method after creating an instance of the extension, allowing you to per-
form any necessary one-time initialization. For
MyExceptionHandler
, the
init
( ) method is left blank. The
handle
( ) method forwards to
exception.jsp
,
which is a simple page with an error message and a link to start over.
Search WWH ::
Custom Search