Search code examples
multithreadingjspjsp-tags

Are custom JSP tags accessed by multiple thread


I have simple Custom JSP tag defined something like:

public class SimpleTag extends TagSupport {
    private static final long serialVersionUID = 1L;

    private String var;
    private Map<String, String> data = new HashMap<String, String>();

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    @Override
    public int doStartTag() throws JspException {
        populateData();
        pageContext.setAttribute(var, data);
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        pageContext.setAttribute(var, null);
        return EVAL_PAGE;
    }

    private void populateData() {
        // add data to "data" map
    }
}

I expose the hashmap to the tag body.

Will the tag be reused by the container (caching/pooling) or accessed by multiple threads? Do I need take extra care in the tag design?

I apologize if it's too basic. I've been unsuccessful with my searches. Thanks in advance.


Solution

  • Classic Tag Handlers could be pooled or not, depending on container;
    Simple Tag Handlers could not be pooled, accordingly with the spec (http://download.oracle.com/otn-pub/jcp/jsp-2_3-mrel2-eval-spec/JSP2.3MR.pdf)

    But anyway, there should not be any threading issue, as a handler is supposed to serve only one request at a time:

    Clarify that a tag handler instance is actively processing only one request at a time; this happens naturally if the tag handler is instantiated afresh through new() invocations, but it requires spelling once tag handler pooling is introduced. This clarification affected Chapter JSP.13.