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 Why do I get Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Why do I get "Cannot forward after response has been committed"?" Watch "Why do I get "Cannot forward after response has been committed"?" New topic
Author

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

Craig Treptow
Greenhorn

Joined: Oct 29, 2007
Posts: 16
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

Joined: Nov 06, 2000
Posts: 177
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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

By the way, why the private method? Why not just have doPost invoke doGet or vice versa?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Jan 10, 2002
Posts: 61315
    
  66

And, why the init and destroy methods? If you don't have anything to do, omit them.
Craig Treptow
Greenhorn

Joined: Oct 29, 2007
Posts: 16
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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Oct 29, 2007
Posts: 16
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

Joined: Oct 29, 2007
Posts: 16
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.
 
GeeCON Prague 2014
 
subject: Why do I get "Cannot forward after response has been committed"?