Patrick Buchanan wrote:
>
> I sent this out yesterday without a single reply. Can anyone help me on this?
> I'm new at Java so any advice would be appreciated.
>
> How do I use a JSP Error Page with a servlet? I assume I use a
> RequestDispatcher object to forward the request and response objects to the JSP,
> but does anyone have a code example of how to accomplish this? Do I have to
> store it as a request attribute and then have code in the JSP to pull it out and
> print it to the screen? What is the easiest way here?
>
> I want to use the same JSP error page for both my controller servlet and the
> associated JSP's.
You can do something like this:
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
...
try {
action.perform(this, request, response);
}
catch (Throwable t) {
request.setAttribute("javax.servlet.jsp.jspException", t);
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
}
If /error.jsp is a JSP page with <%@ page isErrorPage="true" %>, the JSP
container assigns the value of the "javax.servlet.jsp.jspException"
attribute to the implicit "exception" variable. Your JSP page can then
use this object the same way as if it's invoked due to an exception
in a JSP page.
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets