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:

errorfile.jsp:

=====
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page contentType="text/xml; charset=UTF-8" %>
<%@ page session="false" %>
<%
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0);
%>
<?xml version="1.0" encoding="utf-8"?>
<someXmlRoot>
    <errorCode>${requestScope.errorCode}</errorCode>
    <errorMessage>${requestScope.errorMessage}</errorMessage>
</someXmlRoot>
=====

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: ch...@christopherschultz.net
> To: users@tomcat.apache.org
> Subject: [OT] servlet-specific error pages
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> 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
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAk2kuEEACgkQ9CaO5/Lv0PBpqACfVky6ZZvG/Rgpt4XK804jdbei
> JpEAnj/NF2td4NNHoBAbFBRAgsUDkC0v
> =xjBP
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
> 
                                          

Reply via email to