Question :Can I invoke a JSP error page from a servlet? (JSP)
Answer :Yes, you can invoke the JSP error page and pass the exception object to
it from within a servlet. The trick is to create a request dispatcher for the
JSP error page, and pass the exception object as a
javax.servlet.jsp.jspException request attribute. However, note that you
can do this from only within controller servlets. If your servlet opens an
OutputStream or PrintWriter, the JSP engine will throw the following
translation error:
java.lang.IllegalStateException: Cannot forward as OutputStream or Writer
has already been obtained
The following code snippet demonstrates the invocation of a JSP error page
from within a controller servlet:
protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response, String errorPageURL, Throwable e) throws
ServletException, IOException {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse
response) {
try {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}
www.interviewhelper.org is a hope for all the Job seekers. Users can find unlimited here. These range from technical to HR, from functional to behavioral. www.interviewhelper.org contains in the fields like Ajax,VB,Networking,Sharepoint,JEE,Perl,Javascript,Bioinformatics,Classic ASP,Unix,Linux,Accounting,Oracle DBA,Microprocessor,Bluetooth,jms,jme,.net framework,ABAP,ASP,ASP.net,Basic .net Framework,Interview Questions,C language,C++,database,Oracle,Java,PHP,LAMP,SAP etc.
Friday, March 13, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment