自定义 JSP 标签是否被多线程访问

Are custom JSP tags accessed by multiple thread

我有简单的自定义 JSP 标签定义如下:

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
    }
}

我将 hashmap 暴露给标签体。

标签会被容器重用(caching/pooling)还是被多个线程访问?我需要在标签设计上格外小心吗?

如果太基础,我深表歉意。我的搜索一直没有成功。提前致谢。

标签应该在 jsp 上实例化,每个 page.They 的页面呈现应该是请求范围的并且可以包含它们自己的状态并且不应该有任何线程问题,除非你正在做一些事情 strange.But 为了确定这一点,为什么不将标签 class 实例 ID 记录到日志文件中并执行多个请求以了解其行为?

Classic Tag Handlers 可以合并或不合并,具体取决于容器;
简单标签处理程序无法合并,因此符合规范 (http://download.oracle.com/otn-pub/jcp/jsp-2_3-mrel2-eval-spec/JSP2.3MR.pdf)

但无论如何,不​​应该有任何线程问题,因为处理程序应该一次只处理一个请求:

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.

Are custom JSP tags accessed by multiple thread

user3714601 的回答很好 - 实际上标签的使用是线程绑定的,所以您发布的设计应该不会出现任何并发问题。

Will the tag be reused by the container (caching/pooling)

假设这是可能的。这对我几年前从 WebSphere 迁移到 tomcat 的应用程序造成了一些 "interesting" 影响。

在开始使用标签实例之前,请清除上次使用时留下的所有内容。在某些环境中,这不会有任何效果,但我会推荐类似的东西;

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