Are custom JSP tags available across multiple threads

I have a simple custom JSP tag defining 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 am expanding the hashmap to the body.

Will tag reuse by the container (caching / merging) or access to them by multiple threads? Should I take extra care in tag design?

I apologize if it is too simple. I was unable to complete my searches. Thanks in advance.

+3


source to share


3 answers


Classic label handlers can be combined or not, depending on the container.
Simple label handlers could not be combined according to the spec ( http://download.oracle.com/otn-pub/jcp/jsp-2_3-mrel2-eval-spec/JSP2.3MR.pdf )

But there should be no threading issue anyway, since the handler only needs to serve one request at a time:



Clarify that the tag handler instance is actively processing only one request at a time; this happens naturally if a tag handler is created via new (), but this requires spelling after the tag handler is presented. This clarification touched on chapter JSP.13.

+3


source


Are custom JSP tags available across multiple threads

Good answer from user3714601 - the use of the tag is thread-specific, so you shouldn't get any concurrency issues with the design you create.

Whether the tag will be reused by the container (caching / merging)



Let's assume it's possible. This caused some "interesting" effects for an application that I migrated from WebSphere to tomcat a couple of years ago.

Clean up anything from the tag instance that may be left over from previous use before you start working with it. This won't have any effect in some environments, but I would recommend something like this:

private void populateData() {
    data.clear();
    // clear anything else you have
    // add data to "data" map
}

      

+2


source


Tags should be generated based on the jsp page rendering in the page. They should be queried and can contain their own state and there shouldn't be any threading problems unless you do something weird. But to be sure of this, t are you registering the instance id of the tag class in the log file and running multiple queries to understand its behavior?

0


source







All Articles