tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Justin Randall <>
Subject RE: [OT] servlet-specific error pages
Date Wed, 13 Apr 2011 13:17:45 GMT

Hi Chris,

I'm assuming the servlet is in control of generating all of the errors you are implying?

For example, if the servlet doesn't load properly and a 404 would be generated, the servlet
would obviously not be in control of handling this exception and this would would have to
make use of <error-page> directives in web.xml, specifically for <error-code>
404 which would redirect to a JSP that just so happens to be an XML file.

If I've understood what you're looking for correctly I've worked on similar projects in the
past and what's worked well for me is leveraging MVC by using the RequestDispatcher to forward
to relevant JSPs which happen to all be XML files with the contents populated either via JSP
tags or EL.  This way you can catch the exceptions/error conditions in your servlet, set the
appropriate exception information within the request context and the dispatch to the correct
JSP XML page.

For example:


<%@ page trimDirectiveWhitespaces="true" %>
<%@ page contentType="text/xml; charset=UTF-8" %>
<%@ page session="false" %>
    response.setDateHeader ("Expires", 0);
<?xml version="1.0" encoding="utf-8"?>

In your serlvet you would redirect to the JSP as follows:

RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/path/to/errorfile.jsp");
req.setAttribute("errorCode", "500");
req.setAttribute("errorMessage", "My server had an oops!");
dispatcher.forward(req, rsp);

Note the XML file doesn't have a schema associated with it but that's just for brevity of
the example.

I hope this helps!

Justin Randall

> Date: Tue, 12 Apr 2011 16:38:25 -0400
> From:
> To:
> Subject: [OT] servlet-specific error pages
> Hash: SHA1
> All,
> I have a webapp where some of the servlets are expected to return XML
> all the time, even for error conditions. I'd like to be able to set an
> error page for those servlets so the response will be in XML instead of
> HTML like you'd get with the default Tomcat error page or an error page
> we might have configured for the rest of the site.
> I didn't see anything in web.xml that would allow me to set an error
> page for a particular servlet.
> Any good ideas?
> Obviously, I can create a system-wide "error" resource that determines
> the resource that was being used and then dispatch to either an
> HTML-oriented or XML-oriented error page, but I was wondering if
> something like that already existed or if there was a better approach.
> Thanks,
> - -chris
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla -
> iEYEARECAAYFAk2kuEEACgkQ9CaO5/Lv0PBpqACfVky6ZZvG/Rgpt4XK804jdbei
> JpEAnj/NF2td4NNHoBAbFBRAgsUDkC0v
> =xjBP
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message