aspose file tools*
The moose likes JSP and the fly likes Problems with jsp:include Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problems with jsp:include" Watch "Problems with jsp:include" New topic
Author

Problems with jsp:include

Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I'm trying to include a page with a jsp:include, but if I set the attribute flush=true the page doesn't load and if I set it to flush=false it loads but with an exception.

The included page:


The code to include it (we're using a command/controller pattern. fc is the front controller). (alternatively with flush="false")


Exception when flush="false" (the one case the page works)


I appreciate any and all help, and that you took the time to get this far in my post. Thank you! Take care.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

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


Are you doing a forward later on in the request? If so, that's your issue.


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

Joined: Jan 10, 2002
Posts: 61426
    
  67

And... why on earth would you be emitting JavaScript from <cut> tags rather than simply putting the script in the template text?
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Thanks for the tip. I investigated a bit more, removed possible forwarding and now I only have:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Do you have any code anywhere that's closing the output stream? Or even grabbing it from the response?
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
While I was looking for what you described, I stumbled across where the request was being forwarded and deducted that it actually was being forwarded. I took the action needed to make the request redirect instead and, well, no errors... but it doesn't include my jsp page :-(

Front Controller:
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
"Resource included by means of a jsp:include is not permitted to perform a redirect." Does that apply here? What can I do? All I need is to jsp include some code and go through the command/controller pattern!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

If you are trying to forward or redirect after emitting content, you need to sit back and re-think your architecture.

Perhaps this article might be helpful.

And you never did answer my question regarding the use of <cut> to emit template text.
[ June 25, 2007: Message edited by: Bear Bibeault ]
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
As far as the scripting tags go, I thought I had to do it that way. What would be the alternative? Just have it embeded? And I can re-think the architecture all day long, but I'll never get it changed. This is a very small piece of a gigantic web app. Are there any work arounds that would allow me to call commands with an include? Or do I just need to stick the java in the command of the page I'm including from and include the jsp directly?
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
hmm... I've been looking through the controller and, well, no content seems to be emitted before the forward. Just some attributes set here and there.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
New developments! Now the jsp:include doesn't load anything and I get "java.lang.IllegalStateException: Cannot forward after response has been committed" when I try to forward the request. How can I track down where the response has been committed?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

A flush="true" will cause the response to be committed, as will anything that sends part of response to the browser.

And yes, you do not need to use <cut> to emit template text.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I turned flushing off, and now I get a "java.io.IOException: Stream closed"

I've done some heavy debugging and isolating and now all that is being executed after the jsp:include is:

Front controller:


flush="true"


flush="false"

Jsp now looks like:
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
As a side note, when I flush="false" the page actually gets included. Here's the full output from the logs

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

At this point you need to figure out who's closing the stream on you.

What goes on in the request before you hit you "front controller" include file?
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Right now I'm simply requesting "test.jsp" directly. The FC isn't taking over until the jsp include comes about. I simplified the FC further such that all that's happening is



Same issue. Loads the page, but with thee above exception.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Daniel Prene:
The FC isn't taking over until the jsp include comes about.


But does anything happen before that point that could be closing the stream?

If not, I'm not sure what to tell you at this point. The use of JSPs as controllers went out with the last millenium just because of these types of problems.

Your application either needs to be updated with modern accepted patterns, or your JSPs will need to be very very careful not to do anything during a request/response cycle until after all control decisions and dispatching has been accomplished. Otherwise, you start running into precisely the types of problems you are experiencing.

My only advice at this point is to carefully trace everything that's going on during a request prior to the point of failure to see what could be messing up the stream.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I'm not using a jsp as a FC, I'm using a servlet which then invokes commands that then return the name of the jsp that the servlet then forwards. How can I check if the stream is open or closed at a point? I need to know what to be searching for
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Daniel Prene:
I'm not using a jsp as a FC, I'm using a servlet


Now you've got me completely confused. Then what is the purpose of the include? If you are handling the controlling in a servlet prior to forwarding to any JSP, how is a forward occuring after some JSP has executed?

If you are invoking the servlet from a JSP, rather than the other way around, then you are in essence using the JSP as a controller. No JSP should ever be invoked until after all control and dispatching has already taken place.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Let me jump back a few weeks...
Problem: I need to include, in several pages, several lines of jsp code
Solution: use a jsp:include within the jsp
Problem: I need to have a map with some goodies set as an attribute in the request
Solution: Since we're using a CommandFactory/FrontController like pattern, write a command to complement the included page and change the jsp:include to be a request to the fc for it. (fc?obj=cons&cmd=doNav...)
Problem: ... All of this junk about streams closing.
Solution: Write a test.jsp, the only jsp accessed directly, start debugging... run to javaranch.com with my tail between my legs... continue debugging.
[ June 27, 2007: Message edited by: Daniel Prene ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

The basic issue is that you are trying to do control and dispatch from a JSP. I'd back up a few weeks and avoid trying to do so.

If all you need is something to build and set up a scoped variable, explore a custom tag or simple bean to do so.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I need a map of lists generated and available to the jsp (but do not want to resort to using scriptlets). If the jsp:include is just returning the code resulting from a separate request, then why should anything else matter? Isn't it just like I visited the uri in my browser and copy and pasted the resulting code?
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Uh, could I use c:import?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Daniel Prene:
If the jsp:include is just returning the code resulting from a separate request


Inlcudes are tricky beasts that piggy-back on the parent request. Even though they are separate requests, they are set up to look and act like they are a part of the parent request and have some of the same restrictions.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Daniel Prene:
Uh, could I use c:import?


Just uses an include for local resources, so same issues.

What's the issue wiht just using a quick and easy bean?
[ June 27, 2007: Message edited by: Bear Bibeault ]
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
That's not how we generally do things... Although I may misunderstand you. What would it entail?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

You want to instatiate a scoped variable that represents something, right? Then just write a bean that represents it and use <jsp:useBean> to instatiate it.

Sometimes a hammer works well when all you want to do is to bang in a nail.

Or should I interpret "That's not how we generally do things" as "We like to make things as overcomplicated as we can"?

Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Unfortunately, that's a precise interpretation. Imagine a 2000+ line jsp page that is primarily scriptlets and javascript.
 
 
subject: Problems with jsp:include