Enable to forward after response has been committed.
843841May 29 2003 — edited Feb 5 2008
What I need :
IF a exception occurs in my program it should forward to another page where it will be displayed
in a proper format.
Problem :If exception occurs after I commit a response(ie after out.println())
then following exception occurs.
Exception :java.lang.IllegalStateException: Cannot forward after response has been committed
I need a solution But not the following one:
1)Instead of out.println, I can collect all that data in string Buffer and if exception
doesn,t occur then only I will commit my data(ie I will do out.println only if there is no exception).
2)I dont not to use sendDirect instead of forward.
3)There are 100 servlets in which I want to do this changes.What will be the most convienient way.
******************************************************************************************
import java.lang.*;
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class test extends HttpServlet {
public void init(ServletConfig conf) throws ServletException{
super.init(conf);
}
public void destroy(){
super.destroy();
}
public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
String strReferer = request.getRemoteAddr();
request.getRequestDispatcher(Utility.error_page_path+"checkCredentials.jsp").include(request,response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
//I am about to commit my response
out.println("<html>");
out.println("<head>");
//Note My response has already been commited.
//Now I generate a exception.
try{
int i = 8/0;
}catch(Exception e){
throw e ;
}
}catch(Exception e)
{
request.setAttribute("PAGENAME","CLRatePolicyDetails.java");
try{
//Following Statement will not work because exception is generated after response has been commited.
request.getRequestDispatcher(Utility.error_page_path+"renCommonErrorPageOpener.jsp").forward(request,response);
}catch(Exception ee) {
System.out.println(ee);
}
}finally{
out.flush();
out.close();
} //end of finally
} // http session method ends here......
} // class ends here.............