This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes How to include servlets from tags? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to include servlets from tags?" Watch "How to include servlets from tags?" New topic
Author

How to include servlets from tags?

Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
Here's what I'm trying to do
<ct:layout>
Component: <br>
<ct:layoutComponent/>
</ct:layout>
layout: loads a user preference string from database, comma separated, containing mappings to servlets. Eg. comp/news,comp/motd,comp/stocks
Create a instance variable Iterator with the above String tokens.
layoutComponent: use Iterator from parent, and tries to include the servlet.
PROBLEM: the components do not show up in the right place on the website!
ATTEMPTS:
from layoutComponent
// Illegal to flush within a custom tag.
pageContext().include(url);
// Not showing up right
pageContext.getRequestDispatcher(url).include(pageContext.getRequest(), pageContext.getResponse());
Thanks
CHu
[ April 19, 2002: Message edited by: Chu Tan ]
ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
I am lost , I did not understand your _vague_ question.
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
Sorry.. let me reexplain it.. hopefuly its not _vague_ this time..
How to INCLUDE a servlet FROM a JSP tag?
I have tried requestdispatcher.include(request, response), and the servlet's output does not display on the correct position on the HTML page.
I've tried pageContext.include("servletmapping"); and I got an exception thrown. Something about illegal to flush from within a Custom Tag.
Chu
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
This is my solution to include from within tags. It includes an ugly hack which I do not recommend. When running OptimizeIt on the application, the flushes seem to be one of the bottlenecks, not significant though. If anyone can come up with a more elegant solution, please post their code. Thanks.
In the JSP that is going to use the tag that includes another page:

Within the tag, do this

Flushing might not be necessary if your tag stands alone.
[ June 24, 2003: Message edited by: Chu Tan ]
Brusk Baran
Ranch Hand

Joined: Nov 15, 2001
Posts: 132
very nice cheat...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to include servlets from tags?