jQuery in Action, 3rd edition
The moose likes JSP and the fly likes JServ to tomcat migration : j..l..IllegalStateException: Response has already bee Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSP
Bookmark "JServ to tomcat migration : j..l..IllegalStateException: Response has already bee" Watch "JServ to tomcat migration : j..l..IllegalStateException: Response has already bee" New topic

JServ to tomcat migration : j..l..IllegalStateException: Response has already bee

Manoj Pandey
Ranch Hand

Joined: May 03, 2001
Posts: 36
Hi folks,
We are trying to migrate our code from JServ to tomcat 3.2.3/4.0.1 due to this I am facing certain issues. The current bottleneck is this oe.
I get this exception quite often:
Internal Servlet Error:
java.lang.IllegalStateException: Response has already been committed

Now, In the servlet that I am getting this excetion , I do not even try to get the res.getWriter().So there is no question of writing any response to the to output stream. This servlet is basicallu included in a jsp and is the first line of the jsp page.
Here is the code:
HttpSession session = req.getSession(false);
IIFTSessionObject qaiSession = (IIFTSessionObject)session.getValue(MainDefines.IIFT_INFO);

String actual_type=qaiSession.getType();// the type of person as retrieved from the session

String personType=req.getParameter("personType");//the type of person that is authorised to view this page, this is specified as a parameter in the jhtml <servlet> tag
if (personType!=null && personType.indexOf(actual_type)==-1)//unauthorised
{ res.sendRedirect("/apps/highered/iift/framework/unauthorised.html");}
else if (qaiSession==null)//session expired
the course id is given in different files as course_id or courseid
String course_id=req.getParameter("course_id");
if (course_id==null)

/*if (course_id != null && !actual_type.equals("Q") && !actual_type.equals("F")&& !actual_type.equals("C"))
{//the person is not admin. Admin does not have courses attached with him
String user_courses= ","+CourseListHelper.getCourseIdList(req)+",";//helper method returns the courses of th elogged in person a String
if (user_courses.indexOf(","+course_id+",") == -1)//the person is not associated with the requested course id
Log.log("user courses "+user_courses,5,null);
catch(Exception e)
{//session has expired, mostly NullPointerException

hm.. thats a lot of code.
This is hapenning in the entire application in many ways, though the same code ran like a ferrari on JServ .
Can anyone help to make it simpler for me. Has anyone done this sort of migration before, if yes what are the issues to look out for.
Thanks a ton for reading so far
Hope to get some help.

Sun Certified Java Programmer
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

are the page you are redirecting in the same applciation context? If so, you should use getServletContext().getRequestDispatcher("...").forward(request, response) (or .include) in preference in most cases...
That said, as a quick fix try adding a return statement after your sendRedirect() statements.
the short description of the problem you are facing is that when the user asks for a page, you can only send one page back. Once you start sending a response, you are comitted to sending that response and can't change your mind.
Therefore if you have a situation where you start writing a page to the client (out.write or whatever) then decide to go somewhere else (sendRedirect or forward), you are committed to sending the first page and an exception is raised.
More info is available by seraching for "forward include sendredirect" or in the Servlet API
Manoj Pandey
Ranch Hand

Joined: May 03, 2001
Posts: 36
Thanks for the reply Dave.
I will try out the return trick. however I cannot use the forward or include as I am migrating from JServ and I think the version 2.0 of the servlet spec. did not provide the forward mechanism.
Also I have to maintain compatibilty between the code running on JServ and that running on Tomcat. using forward method will beat the pupose beside I will have to recompile 100's of class files.
nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Hi david,
I have the same problem of forwarding the request.You said that adding a return statement is a quick fix after response.sendRedirect().
Can u explain how

Have you checked out Aspose?
subject: JServ to tomcat migration : j..l..IllegalStateException: Response has already bee
It's not a secret anymore!