This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths 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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to include servlets from tags?" Watch "How to include servlets from tags?" New topic

How to include servlets from tags?

Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
Here's what I'm trying to do
Component: <br>
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!
from layoutComponent
// Illegal to flush within a custom tag.
// Not showing up right
pageContext.getRequestDispatcher(url).include(pageContext.getRequest(), pageContext.getResponse());
[ 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
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.
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
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...
subject: How to include servlets from tags?
jQuery in Action, 3rd edition