I have a servlet which uses another java class to generate a report.
An array of databeans is returned from teh report generator to the
calling servlet. What I want to be able to do is write a standard error
handling routine which will, when called, forward onto a JSP and output
the details of the exception.
The only part I can't do is forwarding onto a servlet from a java
class. I've tried using the getRequestDispatcher method but it's not
forwarding on.
Can anybody offer any advice?
> I have a servlet which uses another java class to generate a report.
> An array of databeans is returned from teh report generator to the
[quoted text clipped - 5 lines]
> class. I've tried using the getRequestDispatcher method but it's not
> forwarding on.
I'm trying to piece together exactly what you mean, so let me know if
I've got it wrong. You shouldn't need to actually be in a servlet in
order to forward, so long as you've got the ServletContext so that you
can call getRequestDispatcher on it, and you have the request and
response to pass to forward. If you're doing that and it's not working,
then my best guess is that perhaps the response is already committed?
You can't do a forward after part of the page has already been sent to
the client.
Typically, though, you wouldn't try to forward directly from your
application logic. Application logic properly signals errors with
exceptions, and other navigation choices with return values of various
methods. Your servlet or something in that layer of your application
can translate these components into navigation choices.

Signature
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
"Steve" <stesimpson1973@hotmail.com> said:
>I have a servlet which uses another java class to generate a report.
>An array of databeans is returned from teh report generator to the
[quoted text clipped - 5 lines]
>class. I've tried using the getRequestDispatcher method but it's not
>forwarding on.
I pretty much agree with Chris - don't "export" servlet logic into
your "business logic".
I'd define a new exception class for exceptions thrown from your
report generator, and have the servlet catch and rethrow these exceptions
(at the servlet level you could route these by subclassing ServletException
and rethrow the report generator exceptions as these ServletException
subclasses). Then create an error handler (in web.xml) for this specific
ServletException subclass.
You could also throw a subclass of ServlerException directly from
your report generator. In this case you wouldn't even need the catch/rethrow
in your servlet, but this'd again create an unnecessary tie from the
report generator to the web frontend.

Signature
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Steve - 23 Jun 2006 15:57 GMT
Hi,
Thanks for that. I've followed your advice.
Regards
Steve
> "Steve" <stesimpson1973@hotmail.com> said:
> >I have a servlet which uses another java class to generate a report.
[quoted text clipped - 26 lines]
> PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
> "...cancel my subscription to the resurrection!" (Jim Morrison)
Juha Laiho - 25 Jun 2006 20:55 GMT
Juha Laiho <Juha.Laiho@iki.fi> said:
>"Steve" <stesimpson1973@hotmail.com> said:
>>I have a servlet which uses another java class to generate a report.
[quoted text clipped - 13 lines]
>subclasses). Then create an error handler (in web.xml) for this specific
>ServletException subclass.
Ok, and after writing an example of this for myself, some streamlining.
- the business logic class will throw a "BusinessException" (subclass
of exception), or a subclass of BusinessException, if more fine-grained
exception handling is needed
- the servlet calling the business logic class is prepared to catch
BusinessException, and rethrow it wrapped in a ServletException
- web.xml contains an error-page declaration to catch BusinessException,
and the designated error page (JSP or servlet) will pick up the
exception information from request attributes, and show the desired
error message to the end user
... so, subclassing the ServletException is not needed, and error-page
declaration can be used to trap also "business" exceptions.
Code fragments showing the essentials:
web.xml error-page declaration:
<error-page>
<exception-type>errordemo.business.BusinessException</exception-type>
<location>/errorHandler.jsp</location>
</error-page>
original exception throwing from "business logic class" (real code
would naturally try to do some real work as well):
public void doSomething() throws BusinessException {
throw new BusinessException("Description / detailed error message");
}
catch/rethrow in servlet class:
try {
business.doSomething();
} catch (BusinessException be) {
throw new ServletException(be);
}
accessing the error message from the original exception in the error handler:
<li>
<strong>Error message:</strong>
<%= request.getAttribute("javax.servlet.error.message") %>
</li>
accessing the original exception class (here just to get the class name,
but similar access allows "anything" to be done with the exception):
<li>
<strong>Exception type was:</strong>
<%= ((Class) (request.getAttribute("javax.servlet.error.exception_type"))).getName() %>
</li>
Relevant documentation is the Servlet Specification; see SRV.9.9 (Error
Handling, pp. 75-77) from the specification at
http://www.jcp.org/aboutJava/communityprocess/final/jsr154/index.html .
It describes how the error handling process works, and also contains
descriptions for other available javax.servlet.error.* attributes.

Signature
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)