国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Servlet和ThreadLocal的測試

2019-11-18 12:32:33
字體:
來源:轉載
供稿:網友

  public class TestThreadServlet extends HttpServlet {
  
    PRivate static ThreadLocal thread    = new ThreadLocal();
  
    private int         flag     = 0;
  
    public void doGet( HttpServletRequest request,
  
             HttpServletResponse response)
  
        throws ServletException, IOException {
  
      flag++;
  
      String str = "This is the first String." + new Object();
  
      if (thread.get() == null)
  
        thread.set(str);
  
      PrintWriter out = response.getWriter();
  
      out.println("<p>");
  
      out.println("<BR>flag : " + flag);
  
      out.println("<BR>sessionid : " + request.getSession().getId());
  
      out.println("<BR>servlet : " + this.toString());
  
      out.println("<BR>thread : " + thread.get());
  
      out.println("</p>");
  
    }
  
  }
  
  執行結果:
  
  Session 1:
  
  flag : 2
  sessionid : amGeaiVwKvL9
  servlet : test.other.TestThreadServlet@5f2db0
  thread : This is the first String.java.lang.Object@1ad6b4b
  
  Session 1:
  
  flag : 3
  sessionid : aR3GkcUQoXT-
  servlet : test.other.TestThreadServlet@5f2db0
  thread : This is the first String.java.lang.Object@6214f5
  
  由執行結果可以看出
  
  1 服務器對每個Servlet只創建一個實例。flag不停增加
  
  2 Session范圍內的ThreadLocal中對象唯一。不同的請求,Object的hashCode相同。
  
  3 不同的Session共享ThreadLocal,但內部對象不同
  
  另:后來有人提醒我,實際上在web.xml為同一個servlet配置不同的名字,將會是兩個不同的實例。也就是說,servlet的實例與配置有關。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁德市| 安塞县| 潍坊市| 镇巴县| 剑阁县| 上高县| 南华县| 临沂市| 彭阳县| 铁力市| 建水县| 永靖县| 涿州市| 尼勒克县| 轮台县| 益阳市| 报价| 新巴尔虎左旗| 什邡市| 吉首市| 新余市| 灵丘县| 余庆县| 正阳县| 华坪县| 义乌市| 灵武市| 庆云县| 孝感市| 泸西县| 瓮安县| 漳州市| 南阳市| 台江县| 饶河县| 葵青区| 平利县| 庄河市| 衡南县| 光山县| 汕尾市|