Search code examples
javajspjsp-tagscustom-tag

How do i avoid generating html in a java custom tag handler?


Every example i can find has the tag handler java class generating html and spewing it out with out.print(someHTML);

Is there a way to include a jsp and add attributes to the request instead?


Solution

  • I haven't tried this but it should be possible by obtaining a RequestDispatcher from the Request object:

    public int doStartTag() throws JspException {
        try {
            pageContext.setAttribute("title", "My Title");
            pageContext.getRequest().getRequestDispatcher("/WEB-INF/includes/header.jspf").include(pageContext.getRequest(), pageContext.getResponse());
        }
        catch (IOException e) {
    
        }
        return EVAL_BODY_INCLUDE;
    }
    

    The PageContext also has an include method but that seems to only work for static files, not jsps.