File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Calling include() within a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Calling include() within a JSP" Watch "Calling include() within a JSP" New topic
Author

Calling include() within a JSP

Matthew Webster
Ranch Hand

Joined: May 10, 2001
Posts: 51
Hello peeps,
Could anyone tell me if it is possible to call the include() method (of RequestDispatcher) from inside a JSP, please?
I have found it useful in the past, but cannot seem to get it to work properly at the moment. Only include() from a servlet appears to work, rather than throw an (unhelpful) exception.
My main aim is to have a piece of Java being executed within a JSP which decides at that time which JSP should be included.
I understand this (or something similar) is possible with the jsp:forward tag, but with jsp:include?
Thanks for any help,
Matt.
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

That's exactly what <jsp:include> does. But there's no reason that you should not be able to call the include method within a scriptlet (but why do that when the tag exists to do it for you?)
What part of using <jsp:include> is giving you problems?
hth,
bear
[ January 18, 2003: Message edited by: Bear Bibeault ]

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Matthew Webster
Ranch Hand

Joined: May 10, 2001
Posts: 51
Ah, yes, I have just caught on - sorry.
Quite nifty. Unfortunately, I'm not entirely sure it does what I'm looking for exactly. But then, I'm picky.
Thanks,
Matt.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Matthew Webster:
Ah, yes, I have just caught on - sorry.
Quite nifty. Unfortunately, I'm not entirely sure it does what I'm looking for exactly. But then, I'm picky.

jsp:include should work fine. You can dynamically change the page included in your code by doing something like this in a scriptlet:

and specifying the include page as a rtexpr:
<jsp:include page="<%=forwardPage%>" flush="true" />


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Matthew Webster
Ranch Hand

Joined: May 10, 2001
Posts: 51
Thanx for the help.
I asked because in the past I have used pieces of Java to simply include other JSP's, sometimes in large and complex numbers. This function appeared to disappear when I tried a direct .include() in a JSP under the recent version of Tomcat.
The evidence of this was that the output of the JSP being included appeared before any of the output from the JSP calling the include method.
I am now happy as to an up-to-date methodology, but does anyone have an answer as to why the above happened?

Best regards,
Matt.
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
it may be that the include action was executed (with the result shown at the output) before the contents of the buffer which existed beforehand had been flushed out. the 'flush="true"' indicated by David in his sample <jsp include.../> put things in the right order.
hope this helps.
 
 
subject: Calling include() within a JSP
 
Threads others viewed
include and file path
include files
jsp include directive
Elementary Jsp Include Question
Cross-context JSP includes
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture