java.lang.IllegalStateException: Cannot forward after response has been committed
Joined: Dec 29, 2009
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..........
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.
What happens to the stringBillMonthYear 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.