1

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:

  1. If I need to insert a block of HTML from a tag handler, is this the best way to do it?
  2. 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?
  3. 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?
  4. 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!

Community
  • 1
  • 1
King Skippus
  • 3,801
  • 1
  • 24
  • 24

2 Answers2

0

I found an answer, but I don't know if it's the best answer. I inserted buffer="none" into the page directive of my master jsp page, and now the secondary jsp page falls in line where it's supposed to. Again, I don't know if it's the best answer though, as it seems to me that I should be able to buffer the page. I'll leave the question open for a few days in case someone has a better suggestion.

King Skippus
  • 3,801
  • 1
  • 24
  • 24
0

Woot, I finally found a better answer, and I knew I had to be overlooking something probably relatively simple and fairly obvious. There's another version of include() that takes a boolean as a second parameter. According to the documentation:

If flush is true, The current JspWriter "out" for this JSP is flushed as a side-effect of this call, prior to processing the include. Otherwise, the JspWriter "out" is not flushed.

So my original code above should be in a class that extends SimpleTagHandler, and should be this instead:

public void doTag() {
    PageContext context = (PageContext)getJspContext();
    try {
        // The second parameter below (true) causes the JspWriter to be flushed
        context.include("/views/tags/loginBlock.jsp", true);
    } catch (Exception e) { throw new RuntimeException(e); }
}

Not only is that much shorter and simpler, but it means that I don't have to go putting buffer="none" in all of my JSP page directives. If anyone else runs across this question and answer, I hope you find it useful, because it sure caused me a bunch of grief. :)

King Skippus
  • 3,801
  • 1
  • 24
  • 24