Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do I get "Cannot forward after response has been committed"?

 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing my first servlet, after having gone through the "Head First Servlets & JSP" book.

Based on what I've read in that book and the FAQ here, I should not receive the "Cannot forward after response has been committed" error. Unless of course, I've misunderstood something.

This is a servlet for the TeamSite system (if that matters).

Here is what my servlet is doing:



The JSP looks like this:



If I comment out the view.forward line, I don't get output. If I leave it in, I get the error.

I'm baffled by this and would appreciate any hints you can give me.

Thanks in advance!

Craig
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What calls "processRequest()", and does it do anything with the response? My guess is that the calling routine is writing to the response stream.
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max Rahder wrote:What calls "processRequest()", and does it do anything with the response? My guess is that the calling routine is writing to the response stream.


Here's all of it:

 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Treptow wrote:

Found the answer!



Needs to be:



I don't understand why though. Include gives control back to the servlet, but I didn't think I would want for this purpose? At least the Heads First book makes me think I don't want the include.

Craig
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it doesn't. An include is not a suitable substitute for the forward. The problem lies elsewhere, though I cannot see it upon inspection.

This is a very (and I do mean very) common scenario, so it should all work. There must be more to it than what is posted.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why the private method? Why not just have doPost invoke doGet or vice versa?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CSClient client = (CSClient)request.getAttribute( "iw.csclient");

The fact that the very first thing you are doing in the servlet is to fetch a request-scoped variable is highly suspicious that something else is executing before the servlet. Otherwise, there would be no opportunity for there to even be such a variable in request scope.

What is it that you're not telling us?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, why the init and destroy methods? If you don't have anything to do, omit them.
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:By the way, why the private method? Why not just have doPost invoke doGet or vice versa?


I simply started with a sample provided with the TeamSite product.
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:And, why the init and destroy methods? If you don't have anything to do, omit them.


This was also due to starting with a sample provided by the vendor.
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:No it doesn't. An include is not a suitable substitute for the forward. The problem lies elsewhere, though I cannot see it upon inspection.

This is a very (and I do mean very) common scenario, so it should all work. There must be more to it than what is posted.


Include doesn't give control back? Going from page 207 in the Head First book:

the include() method sends the request to something else (typically another servlet) to do some work and then comes back to the sender!


Is the book wrong, or have I misunderstood something?
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
CSClient client = (CSClient)request.getAttribute( "iw.csclient");

The fact that the very first thing you are doing in the servlet is to fetch a request-scoped variable is highly suspicious that something else is executing before the servlet. Otherwise, there would be no opportunity for there to even be such a variable in request scope.

What is it that you're not telling us?


This is also from a working sample provided by the sample. I need a CSClient object to do almost anything with the application.

I'm not deliberately withholding information, but I could be easily ignorant of some key piece that would help you out.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoever provided those sample writes extremely sloppy code, in my opinion.

With regards to the scoped variable, if the servlet is the first thing that executes in the request, your fetch will always return null as there is no way that anything will have had an opportunity to set a variable into the request.

Are there filters configured that execute before the servlet?

If your forward is failing -- and again, using an include instead of a forward is sloppy sloppy sloppy -- it's because something is tinkering with the request other than the code you have in the servlet.
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Whoever provided those sample writes extremely sloppy code, in my opinion.

With regards to the scoped variable, if the servlet is the first thing that executes in the request, your fetch will always return null as there is no way that anything will have had an opportunity to set a variable into the request.

Are there filters configured that execute before the servlet?

If your forward is failing -- and again, using an include instead of a forward is sloppy sloppy sloppy -- it's because something is tinkering with the request other than the code you have in the servlet.


What you say about that scoped variable is making some sense. I guess I'll poke around on the vendor's site and see if I can learn something else. All the examples I've seen use the include.

No filters:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless there is a filter to set up the CSClient variable, it will never exist.

Are you sure that you're not supposed to be looking in the session or someplace else for it?
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Unless there is a filter to set up the CSClient variable, it will never exist.

Are you sure that you're not supposed to be looking in the session or someplace else for it?


No, I'm not sure, just starting with the sample provided. The docs they provide are very vague about these details. They basically say you can use JSP, or servlets, see the examples.

In their sample web.xml I see this:



That's for the working sample that I started with.

Guess it's time to read about filters and what they do...
 
Craig Treptow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Unless there is a filter to set up the CSClient variable, it will never exist.

Are you sure that you're not supposed to be looking in the session or someplace else for it?


Their docs say CSClient represents the user's session.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic