> Is there a way of instantiating a JSP's 'exception' variable ? [from a
servlet]
If in the course of normal application events your servlet encounters an
unexpected exception, you have the option of passing the exception on to a
JSP error page. This keeps all of your exception handling and error
processing consistent throughout the application, regardless of rather
errors crop up in the JSP pages themselves or your servlets. Simply catch
the Exception (which can be any subclass of Throwable) and put it into the
request object under the name "javax.servlet.jsp.jspException". Then use the
RequestDispatcher to forward your request on to your error handling page.
For example:
String username = req.getParameter("user");
if (username == null)
req.setAttribute("javax.servlet.jsp.jspException", new Exception("no
username!"));
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/error.jsp");
rd.forward(req, res);
The errorHandler.jsp page in this example should be defined as JSP error
page as normal. When we stuff an exception object into the request with that
attribute name (javax.servlet.jsp.jspException) the error page will
automatically create the implicit exception object, and error handling can
proceed. There is no difference between an exception created by our servlet
in this example and an exception being generated by an error in another JSP
page.
Duane Fields
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html