jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes java.lang.IllegalStateException: Cannot forward after response has been committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.IllegalStateException: Cannot forward after response has been committed" Watch "java.lang.IllegalStateException: Cannot forward after response has been committed" New topic
Author

java.lang.IllegalStateException: Cannot forward after response has been committed

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
Hi,
I am new to servlets ......
I am transferring the contents from Servlets to Jsp..
I am getting only one loop value from the database to Servlets ,the loop is not going further and it is throwing the exception related to Dispatcher..........
Thanks.....
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Your code isn't indented properly so it's hard to tell. Are you trying to forward from within the while loop?

If so, that's your problem.
Once you forward, you can't do anything else that writes to the output stream (including forwarding again).

Build up your data structures, then forward.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I have a few comments which I hope will be helpful.

Your indentation makes the code harder to follow.

Who do you think will use the non-zero-arg constructors? The Servlet class is going to be instantiated by the container, using the zero-arg constructor - and it must be this way. In other words, you can just go and create one yourself (using new Simple(...) for example). (later edit: I see where they are used now and will comment on that below...).

In your zero-arg constructor, you're not calling the super's constructor:
In your doPost() method, you're creating an instance of the Servlet?!?

No, no, no, no... You're using the Servlet class as though it were a JavaBeans class (at least that what it's looks like you're trying to do). If you wish to use a JavaBeans object, create a separate class for that. In fact, it looks like you're creating a completely separate Simple instance for each variable. I just can't imaging what you're thinking there

The error you say you're getting is usually due to writing some response to the client, then trying to forward. Although I don't see you doing that here, with all the noise in that code, it's rather hard to tell.

Why are you catching any exceptions in the doGet() method? Don't do that. Just call doPost(request, response); and be done with it.

First things first - follow a tutorial on Servlets and JSP. Get a good understanding of how they work, how you pass data around between the servlet class and the JSP page, etc. Ignore the database work for now. Come back to that once you have a better handle on Servlets and JSP in general.

Best of luck. Please go through a tutorial. If you learned what you're doing in your example from a tutorial, find a better tutorial

Edit: Ahh, yes. As Ben said, you're forwarding within the while loop, and so trying to forward multiple times. This is not the way to do it.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What happens to the string BillMonthYear once the constructor has ended?

(By the way, Java naming conventions include naming non-static final variables with a lower-case letter.)Here you're creating a new simple, but setting properties equal to the value returned by that property's getter, which won't have anything initialized yet.Making this static (or even plain old instance variables) will lead to immediate threading issues.Or possibly not, since you seem to declare them twice--decide which you're going to use (hint: not the static ones) and remove the rest.You don't need to quote the column names in a SQL select statement.Whoa... if these are all for the same entity, then the JavaBean (*not* a servlet, as was pointed out to you) should be created once, and initialized to the row's values.

Right now you're creating an ArrayList of servlets, each completely uninitialized because the constructor that takes a string does essentially nothing.In general it's a good idea to name variables something that makes sense in context--using single-letter alphabet names is almost never a good idea (with some notable exceptions).

Also consider some minor refactoring; it will make things much cleaner and easier for others to understand. My next post will contain the properly-formatted code.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The properly-formatted code:There's a lot of work to be done here.
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
thanks for the formatted code...

the exception runs away by using a "return;" statement after line3...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Line 3 is an import statement.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.IllegalStateException: Cannot forward after response has been committed