Wednesday, January 06, 2010

Invoking a JSP error page from a servlet



protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
try {
//try block
}
catch(Exception ex) {
req.setAttribute("javax.servlet.ex",ex);//store exception as request attribute.
ServletConfig sConfig = getServletConfig();
ServletContext sContext = sConfig.getServletContext();
sContext.getRequestDispatcher("/jsp/ErrorPage.jsp").forward(req, resp);

/*request with the exception stored as an attribute forwaded to the "ErrorPage.jsp".
*/
ex.printStackTrace();
}
}