---
title: "jsp自定义标签 线程安全 "
author: "klcwt"
date: "2010-08-28T14:21:27Z"
tags: []
categories: ["编程语言"]
slug: "jsp"
draft: false
lastmod: "2010-08-28T14:33:30Z"
id: 749652
url: "https://klcwt.iteye.com/blog/749652"
---

我们在编写自定义标签的时候设置属性如下

```
public class InputTag extends TagSupport {

	private static final long serialVersionUID = 1L;

	private String onclick;

	private String style;

	private String styleClass;

	private String value;

	private String id;
```

 

在页面上如果同时使用两个标签：

```
	<h3:input type="button" onclick="myFun()" name="name" id="id"
			style="style" styleClass="styleClass" value="中国人" url="url"
			pid="pid" isValidated="true">
		中国人
		</h3:input>
		
		<h3:input type="button" onclick="myFun()" name="name" id="id"
			style="style" styleClass="styleClass" value="美国人" url="url"
			pid="pid" isValidated="true">
		</h3:input>
```

 

从后台发现打印的InpuTag都是**同一个对象!**

 

发现这个后，我十分担心线程安全问题！比如这些getType();setType(); ！

 

于是就看了下jsp生成的Servlet源代码

```
    out.write("\t<body>\r\n");
      out.write("\t\t");
       //调用InputTag
      if (_jspx_meth_h3_005finput_005f0(_jspx_page_context))
        return;
      out.write("\r\n");
      out.write("\t\t\r\n");
      out.write("\t\t");
      //调用InputTag
      if (_jspx_meth_h3_005finput_005f1(_jspx_page_context))
        return;
      out.write("\r\n");
      out.write("\t</body>\r\n");
```

 

再接着看\_jspx\_meth\_h3\_005finput\_005f0方法

```
  private boolean _jspx_meth_h3_005finput_005f0(PageContext _jspx_page_context)
          throws Throwable {
    PageContext pageContext = _jspx_page_context;
    JspWriter out = _jspx_page_context.getOut();
    //  h3:input
    tag.InputTag _jspx_th_h3_005finput_005f0 = (tag.InputTag) _005fjspx_005ftagPool_005fh3_005finput_0026_005fvalue_005furl_005ftype_005fstyleClass_005fstyle_005fpid_005fonclick_005fname_005fisValidated_005fid.get(tag.InputTag.class);
    _jspx_th_h3_005finput_005f0.setPageContext(_jspx_page_context);
    _jspx_th_h3_005finput_005f0.setParent(null);
    // /button2.jsp(12,2) name = type type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
    _jspx_th_h3_005finput_005f0.setType("button");
    // /button2.jsp(12,2) name = onclick type = java.lang.String reqTime = false required = true fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null    _jspx_th_h3_005finput_005f0.setPid("pid");
    // /button2.jsp(12,2) name = isValidated type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
    _jspx_th_h3_005finput_005f0.setIsValidated("true");
    int _jspx_eval_h3_005finput_005f0 = _jspx_th_h3_005finput_005f0.doStartTag();
    if (_jspx_eval_h3_005finput_005f0 != javax.servlet.jsp.tagext.Tag.SKIP_BODY) {
      do {
        out.write("\r\n");
        out.write("\t\t中国人\r\n");
        out.write("\t\t");
        int evalDoAfterBody = _jspx_th_h3_005finput_005f0.doAfterBody();
        if (evalDoAfterBody != javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN)
          break;
      } while (true);
    }
    if (_jspx_th_h3_005finput_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
      _005fjspx_005ftagPool_005fh3_005finput_0026_005fvalue_005furl_005ftype_005fstyleClass_005fstyle_005fpid_005fonclick_005fname_005fisValidated_005fid.reuse(_jspx_th_h3_005finput_005f0);
      return true;
    }
    _005fjspx_005ftagPool_005fh3_005finput_0026_005fvalue_005furl_005ftype_005fstyleClass_005fstyle_005fpid_005fonclick_005fname_005fisValidated_005fid.reuse(_jspx_th_h3_005finput_005f0);
    return false;
  }
```

最关键就是这句了,看他如何获得自定义标签对象： tag.InputTag \_jspx\_th\_h3\_005finput\_005f0 = (tag.InputTag) \_005fjspx\_005ftagPool\_005fh3\_005finput\_0026\_005fvalue\_005furl\_005ftype\_005fstyleClass\_005fstyle\_005fpid\_005fonclick\_005fname\_005fisValidated\_005fid.get(tag.InputTag.class);

解释下：

 \_jspx\_th\_h3\_005finput\_005f0 是InputTag 的实例 也就是&lt;h3:input.

而

005fjspx\_005ftagPool\_005fh3\_005finput\_0026\_005fvalue\_005furl\_005ftype\_005fstyleClass\_005fstyle\_005fpid\_005fonclick\_005fname\_005fisValidated\_005

是TagHandlerPool的实例

 

自定义标签是通过这个TagHandlerPool.get 来获取的！

举一反三，有借就有还TagHandlerPool.reuse用来回收这个对象！

 

 

TagHandlerPool.get 

TagHandlerPool.reuse

方法如下：

 

```
   /**
     * Gets the next available tag handler from this tag handler pool,
     * instantiating one if this tag handler pool is empty.
     *
     * @param handlerClass Tag handler class
     *
     * @return Reused or newly instantiated tag handler
     *
     * @throws JspException if a tag handler cannot be instantiated
     */
    public Tag get(Class handlerClass) throws JspException {
	Tag handler = null;
        synchronized( this ) {
            if (current >= 0) {
                handler = handlers[current--];
                return handler;
            }
        }

        // Out of sync block - there is no need for other threads to
        // wait for us to construct a tag for this thread.
        try {
            Tag instance = (Tag) handlerClass.newInstance();
            AnnotationHelper.postConstruct(annotationProcessor, instance);
            return instance;
        } catch (Exception e) {
            throw new JspException(e.getMessage(), e);
        }
    }
```

 

```
    /**
     * Adds the given tag handler to this tag handler pool, unless this tag
     * handler pool has already reached its capacity, in which case the tag
     * handler's release() method is called.
     *
     * @param handler Tag handler to add to this tag handler pool
     */
    public void reuse(Tag handler) {
        synchronized( this ) {
            if (current < (handlers.length - 1)) {
                handlers[++current] = handler;
                return;
            }
        }
        // There is no need for other threads to wait for us to release
        handler.release();
        if (annotationProcessor != null) {
            try {
                AnnotationHelper.preDestroy(annotationProcessor, handler);
            } catch (Exception e) {
                log.warn("Error processing preDestroy on tag instance of " 
                        + handler.getClass().getName(), e);
            }
        }
    }
```

 

 

现在就明白了所有的tag对象都是有一个对象池来维护的，一是方便了重用，而是做到了线程同步。

 

 总结：自定义标签是线程安全的，同时也是可重用的！

 

同时又有另一个疑问

 synchronized( this ) {  
            if (current &gt;= 0) {  
                handler = handlers\[current--];  
                return handler;  
            }  
        }

感觉这种方法可能只能在一个页面上共享，另一个页面上的tag估计是不能共享的！

 

后来看了下生成的servelt代码

如下：

```
public void _jspInit() {
    _tagpool = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }
```

 可以看到\_tagpool 是根据ServletConifg来生成的

TagHandlerPool.getTagHandlerPool代码如下

```
 public static TagHandlerPool getTagHandlerPool( ServletConfig config) {
        TagHandlerPool result=null;

        String tpClassName=getOption( config, OPTION_TAGPOOL, null);
        if( tpClassName != null ) {
            try {
                Class c=Class.forName( tpClassName );
                result=(TagHandlerPool)c.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
                result=null;
            }
        }
        if( result==null ) result=new TagHandlerPool();
        result.init(config);

        return result;
    }
```