Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes IllegalStateException: Cannot forward after response has been committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "IllegalStateException: Cannot forward after response has been committed" Watch "IllegalStateException: Cannot forward after response has been committed" New topic
Author

IllegalStateException: Cannot forward after response has been committed

Padhma Prema
Greenhorn

Joined: Dec 23, 2008
Posts: 15
Hi,

I am getting this error:
HTTP Status 500 - java.lang.IllegalStateException: Cannot forward after response has been committed

I have read few forum solutions, but still could not get this working.

I am trying to create a web application that creates a dynamic form. It takes a value from the user, and this value sets the
number of HTML controls displayed on a second page. It then displays the contents of these controls on a
third page.

when i try to run the web application, i get the error.

Servlet 1:




Servlet 2:

public class Assignment2Part2Redirect extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

request.getAttribute("child11");


out.println("<html>");
out.println("<body bgcolor='pink'>");
java.util.Date today = new java.util.Date();
out.println("Today's date is:" + " " + today);
out.println("

You have reached your destination.");
// out.println("

Your child 1 is:" + " " + ch1);
out.println("</body>");
out.println("</html>");
}
}

XML:

<web-app>

<servlet>
<servlet-name>A</servlet-name>
<servlet-class>Assignment2Part2</servlet-class>
</servlet>

<servlet-mapping>
<url-pattern>/dynamicPage.do</url-pattern>
<servlet-name>A</servlet-name>
</servlet-mapping>




<servlet>
<servlet-name>B</servlet-name>
<servlet-class>Assignment2Part2Redirect</servlet-class>
</servlet>

<servlet-mapping>
<url-pattern>/Assignment2Part2Redirect</url-pattern>
<servlet-name>B</servlet-name>
</servlet-mapping>




/web-app>



---
Note: Redirection from servlet 1 to servlet 2 is fine...but i am unable to figure out the error 500.

Please HELP.

Padhma
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Padhma Prema wrote:
HTTP Status 500 - java.lang.IllegalStateException: Cannot forward after response has been committed

This exception is fairly self-explaining. You cannot forward the request (or redirect the response) when the response has been committed.

A response is been committed when you already have invoked a redirect before, or when you already have written more than 8KB to the outputstream, or when you have invoked flush() on the outputstream.

With that information, you must be able to solve the problem by just reading the code step by step.
Padhma Prema
Greenhorn

Joined: Dec 23, 2008
Posts: 15
I have looked into the explanation for this, but i am unable to figure out the issue.
please help me here.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
I have not looked at your code closely as it is unformatted and contains a lot of clutter, but a common mistake by starters is that they think that invoking sendRedirect() or forward() will immediately stop the execution of the remaining part of the method where in it is been invoked. This assumption is wrong. They just behave as every other Java method call and does not have some additional magic powers. Just think logical and check your code step by step.
Ashwin Pai
Ranch Hand

Joined: May 20, 2008
Posts: 90
Padhma Prema wrote:I have looked into the explanation for this, but i am unable to figure out the issue.
please help me here.


Check closely and you will find it.
Hint: you are doing both redirect and forward!! one after the other.. see if you can catch it in the code!!

Ashwin


The only way to make your dream come true is to wake up.
Padhma Prema
Greenhorn

Joined: Dec 23, 2008
Posts: 15
Thank you.
I looked into my code and Ihave removed the redirect, and have kept behind only the forward.

I am not sure if i am doing the right thing though.
This is what I need...but the data from servlet 1 should be displayed in another page via servlet 2. so i am trying to process data and forward it to servlet 2.

But when i run this appliucation, from page 1 it directly goes to page 3, skipping servlet 1 processing.

If you have any good online books or material i can read and better understand, could you please post it.
I have been reading many posts and web articles in vain.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You´re doing a redirect AND a forward consecutively? This is indeed not going to work. Do the one OR the other. How to do it is fairly trivial. Just use an if/else block or add a return statement.
Padhma Prema
Greenhorn

Joined: Dec 23, 2008
Posts: 15
No i am not doing a redirect any more. I have posted my code , withing the tags.

"I am trying to create a web application that creates a dynamic form. It takes a value from the user, and this value sets the
number of HTML controls displayed on a second page. It then displays the contents of these controls on a
third page."
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
@Padhma Prema
Since you have generated a full html page in servlet1, why are you forwarding to servlet2, without doing anything with whatever was written to the response in servlet1. In servlet2, you again start writing the html page, right from the beginning. What about the stuff written to the response in servlet1


Thanks and Regards
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Ah I see your formatted code. You´re writing to the response before forwarding the request. Don´t do that. Use JSP for presentation, not servlets. Using out.println in servlet/jsp is a bad practice.
Padhma Prema
Greenhorn

Joined: Dec 23, 2008
Posts: 15
If i remove the dispatcher line from servlet 1 code, i am able to move from index.html to servlet page whichwill show me textboxes to enter names of the children.

Now, i want to take these names from servlet one and on the click of submit button in servlet one,i want to display the names in a 3rd page.
i was under the impression, you can display the names in 3rd page only by introducing a different another servlet
Padhma Prema
Greenhorn

Joined: Dec 23, 2008
Posts: 15
but, if i dont write to response to a writer, then how will i display the textbox's where i should be entering the names?
These names on the click of submut button in the servelt code, must display the names on thenext page.

Sorry, probably i am missing out an entier logic here..but i have asking these questions coz i still confused.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6

Using servlets for presentation part is a real tough thing and not very recommended. The following approach might be good. A jsp/html page where user enters data, form is submitted control goes to servlet, servlet stores the data in some bean classes, stores the bean object in request/session/application scope(as required by you) and forwards the request to a jsp, jsp invokes the bean(earlier saved by the servlet) , retrieves data and sends the response back to the client.


Swastik
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: IllegalStateException: Cannot forward after response has been committed