I have run into a wall trying to find an answer to this question on how to avoid generating HTML within a custom tag handler. Like this questioner, I'm trying to avoid a bunch of println()
statements in my tag handler, and I'd really like to pull in the data from another .jsp file.
Accordingly, I tried the method suggested:
public int doStartTag() throws JspException {
try {
pageContext.getResponse().flushBuffer();
pageContext.getRequest()
.getRequestDispatcher("/views/tags/loginBlock.jsp")
.include(pageContext.getRequest(), pageContext.getResponse());
} catch (Exception e) { throw new RuntimeException(e); }
return EVAL_BODY_INCLUDE;
}
What I'm getting, however, is the content of loginBlock.jsp above the output of the jsp in which the tag is located, not inserted in place of the tag like I expected, even with the call to flushBuffer()
.
I have a questions that I'm trying to get answers to, but the gods of Google aren't kind to me tonight:
- If I need to insert a block of HTML from a tag handler, is this the best way to do it?
- In the original answer, the poster uses a classic tag handler. Is there some way to do it using simple tag handler (that is, in a class that extends SimpleTagSupport) instead?
- Even though the questioner in the question liked above accepted an answer, I'm not able to duplicate successful results. Am I missing something that the questioner picked up on that I'm leaving out?
- Am I just way off base here in some way I'm not even aware of? I'm kind of new to the whole jsp/servlets thing, and I'm not 100% sure this design pattern is what I should be using. I'm basically trying to design jsp files that call back-end Java that performs some business logic and generates resultant HTML, but like I said above, I don't want to stick a bunch of
println()
calls in my Java tag handlers.
Thanks for any help and/or insight!