• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The properly-formatted code:There's a lot of work to be done here.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the formatted code...

the exception runs away by using a "return;" statement after line3...
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 3 is an import statement.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic