jsp自定义标签 线程安全

6 篇文章 0 订阅

 

转自:http://klcwt.iteye.com/blog/749652

 

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

Java代码    收藏代码
  1. public class InputTag extends TagSupport {  
  2.   
  3.     private static final long serialVersionUID = 1L;  
  4.   
  5.     private String onclick;  
  6.   
  7.     private String style;  
  8.   
  9.     private String styleClass;  
  10.   
  11.     private String value;  
  12.   
  13.     private String id;  

 

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

Html代码    收藏代码
  1. <h3:input type="button" onclick="myFun()" name="name" id="id"  
  2.         style="style" styleClass="styleClass" value="中国人" url="url"  
  3.         pid="pid" isValidated="true">  
  4.     中国人  
  5.     </h3:input>  
  6.       
  7.     <h3:input type="button" onclick="myFun()" name="name" id="id"  
  8.         style="style" styleClass="styleClass" value="美国人" url="url"  
  9.         pid="pid" isValidated="true">  
  10.     </h3:input>  

 

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

 

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

 

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

Java代码    收藏代码
  1. out.write("\t<body>\r\n");  
  2.   out.write("\t\t");  
  3.    //调用InputTag  
  4.   if (_jspx_meth_h3_005finput_005f0(_jspx_page_context))  
  5.     return;  
  6.   out.write("\r\n");  
  7.   out.write("\t\t\r\n");  
  8.   out.write("\t\t");  
  9.   //调用InputTag  
  10.   if (_jspx_meth_h3_005finput_005f1(_jspx_page_context))  
  11.     return;  
  12.   out.write("\r\n");  
  13.   out.write("\t</body>\r\n");  

 

再接着看_jspx_meth_h3_005finput_005f0方法

Java代码    收藏代码
  1.   private boolean _jspx_meth_h3_005finput_005f0(PageContext _jspx_page_context)  
  2.           throws Throwable {  
  3.     PageContext pageContext = _jspx_page_context;  
  4.     JspWriter out = _jspx_page_context.getOut();  
  5.     //  h3:input  
  6.    <span style="color: #ff0000;"> tag.InputTag <span style="color: #0000ff;">_jspx_th_h3_005finput_005f0</span> = (tag.InputTag) _005fjspx_005ftagPool_005fh3_005finput_0026_005fvalue_005furl_005ftype_005fstyleClass_005fstyle_005fpid_005fonclick_005fname_005fisValidated_005fid.get(tag.InputTag.class);</span>  
  7.     _jspx_th_h3_005finput_005f0.setPageContext(_jspx_page_context);  
  8.     _jspx_th_h3_005finput_005f0.setParent(null);  
  9.     // /button2.jsp(12,2) name = type type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null  
  10.     _jspx_th_h3_005finput_005f0.setType("button");  
  11.     // /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");  
  12.     // /button2.jsp(12,2) name = isValidated type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null  
  13.     _jspx_th_h3_005finput_005f0.setIsValidated("true");  
  14.     int _jspx_eval_h3_005finput_005f0 = _jspx_th_h3_005finput_005f0.doStartTag();  
  15.     if (_jspx_eval_h3_005finput_005f0 != javax.servlet.jsp.tagext.Tag.SKIP_BODY) {  
  16.       do {  
  17.         out.write("\r\n");  
  18.         out.write("\t\t中国人\r\n");  
  19.         out.write("\t\t");  
  20.         int evalDoAfterBody = _jspx_th_h3_005finput_005f0.doAfterBody();  
  21.         if (evalDoAfterBody != javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN)  
  22.           break;  
  23.       } while (true);  
  24.     }  
  25.     if (_jspx_th_h3_005finput_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {  
  26.       <span style="color: #ff0000;">_005fjspx_005ftagPool_005fh3_005finput_0026_005fvalue_005furl_005ftype_005fstyleClass_005fstyle_005fpid_005fonclick_005fname_005fisValidated_005fid.reuse(_jspx_th_h3_005finput_005f0);</span>  
  27.       return true;  
  28.     }  
  29. <span style="color: #ff0000;">    _005fjspx_005ftagPool_005fh3_005finput_0026_005fvalue_005furl_005ftype_005fstyleClass_005fstyle_005fpid_005fonclick_005fname_005fisValidated_005fid.reuse(_jspx_th_h3_005finput_005f0);</span>  
  30.     return false;  
  31.   }  

最关键就是这句了,看他如何获得自定义标签对象: 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 的实例 也就是<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

方法如下:

 

Java代码    收藏代码
  1. /** 
  2.   * Gets the next available tag handler from this tag handler pool, 
  3.   * instantiating one if this tag handler pool is empty. 
  4.   * 
  5.   * @param handlerClass Tag handler class 
  6.   * 
  7.   * @return Reused or newly instantiated tag handler 
  8.   * 
  9.   * @throws JspException if a tag handler cannot be instantiated 
  10.   */  
  11.  public Tag get(Class handlerClass) throws JspException {  
  12. g handler = null;  
  13.      synchronizedthis ) {  
  14.          if (current >= 0) {  
  15.              handler = handlers[current--];  
  16.              return handler;  
  17.          }  
  18.      }  
  19.   
  20.      // Out of sync block - there is no need for other threads to  
  21.      // wait for us to construct a tag for this thread.  
  22.      try {  
  23.          Tag instance = (Tag) handlerClass.newInstance();  
  24.          AnnotationHelper.postConstruct(annotationProcessor, instance);  
  25.          return instance;  
  26.      } catch (Exception e) {  
  27.          throw new JspException(e.getMessage(), e);  
  28.      }  
  29.  }  

 

Java代码    收藏代码
  1. /** 
  2.  * Adds the given tag handler to this tag handler pool, unless this tag 
  3.  * handler pool has already reached its capacity, in which case the tag 
  4.  * handler's release() method is called. 
  5.  * 
  6.  * @param handler Tag handler to add to this tag handler pool 
  7.  */  
  8. public void reuse(Tag handler) {  
  9.     synchronizedthis ) {  
  10.         if (current < (handlers.length - 1)) {  
  11.             handlers[++current] = handler;  
  12.             return;  
  13.         }  
  14.     }  
  15.     // There is no need for other threads to wait for us to release  
  16.     handler.release();  
  17.     if (annotationProcessor != null) {  
  18.         try {  
  19.             AnnotationHelper.preDestroy(annotationProcessor, handler);  
  20.         } catch (Exception e) {  
  21.             log.warn("Error processing preDestroy on tag instance of "   
  22.                     + handler.getClass().getName(), e);  
  23.         }  
  24.     }  
  25. }  

 

 

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

 

 总结:自定义标签是线程安全的,同时也是可重用的!


 

同时又有另一个疑问

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

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

 

后来看了下生成的servelt代码

如下:

Java代码    收藏代码
  1. public void _jspInit() {  
  2.     _tagpool = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());  
  3.     _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();  
  4.     _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());  
  5.   }  

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

TagHandlerPool.getTagHandlerPool代码如下

Java代码    收藏代码
  1. public static TagHandlerPool getTagHandlerPool( ServletConfig config) {  
  2.        TagHandlerPool result=null;  
  3.   
  4.        String tpClassName=getOption( config, OPTION_TAGPOOL, null);  
  5.        if( tpClassName != null ) {  
  6.            try {  
  7.                Class c=Class.forName( tpClassName );  
  8.                result=(TagHandlerPool)c.newInstance();  
  9.            } catch (Exception e) {  
  10.                e.printStackTrace();  
  11.                result=null;  
  12.            }  
  13.        }  
  14.        if( result==null ) result=new TagHandlerPool();  
  15.        result.init(config);  
  16.   
  17.        return result;  
  18.    }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
常考面试题 1.讲下servlet的执行流程。 Servlet的执行流程也就是servlet的生命周期,当服务器启动的时候生命周期开始,然后通过init()《启动顺序根据web.xml里的startup-on-load来确定加载顺序》 方法初始化servlet,再根据不同请求调用doGet或doPost方法,最后再通过destroy()方法进行销毁。 2.Get和Post的区别 在页面提交数据时,get的数据大小有限制4k,post没有限制,get请求提交的数据会在地址栏显示,post不显示,所以post比get安全. 3.有三台服务器,如果在一台服务器上登陆了这个用户,其他两台就不能再登陆这个用户,使用session共享,你是怎么做的。 把所有的session的数据保存到Mysql服务器上,所有Web服务器都来这台Mysql服务器来获取Session数据。 4.写一个自定义标签要继承什么类 SimpleTagSupport,一般调用doTag方法 或者实现SimpleTag接口 5.Jsp如何处理json 在 jsp 中处理 JSON,通常需要配套使用 JQuery 控件,并且导入一些 Common jar 包。 使用 JQuery 控件是因为它能有效的解析并且展示 JSON 数据, 导入Common 则是因为 Java 中的对象并不是纯粹的数据,需要通过这些 Jar 包的处理使之转化成真实数据。 6.如何处理Servlet的线程不安全问题 1.最简单的就是不使用字段变量, 2.使用final修饰变量, 3.线程安全就是多线程操作同一个对象不会有问题,线程同步一般来保护线程安全, 所以可以在Servlet的线程里面加上同步方法或同步块。 (Synchronized)可以保证在同一时间只有一个线程访问,(使用同步块会导致性能变差,最好不去使用实例变量) 7.Jsp的重定向和转发的流程有什么区别 重定向是客户端行为,转发是服务器端行为 重定向时服务器产生两次请求,转发产生一次请求,重定向时可以转发到项目以外的任何网址,转发只能在当前项目里转发 重定向会导致request对象信息丢失。转发则不会 转发的url不会变,request.getRequestDispatch().forward() 重定向的url会改变,response.getRedirect().sendRedirect();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值