日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Tomcat源碼分析(四)--容器處理鏈接之責任鏈模式

系統 2022 0

本系列轉載自?http://blog.csdn.net/haitao111313/article/category/1179996?
目標:在這篇文章希望搞明白 connector.getContainer().invoke(request,?response); 調用容器的invoke后是怎么傳遞到 servlet或者jsp的?

? ?由上篇文章 Tomcat源碼分析(三)--連接器是如何與容器關聯的? 可知, connector.getContainer()得到的容器應該是 StandardEngine(其實應該是由server.xml文件配置得到的,這里先假定是 StandardEngine ), StandardEngine沒有invoke方法,它繼承與ContainerBase(事實上所有的容器都繼承于ContainerBase,在ContainerBase類有一些容器的公用方法和屬性),抽象類 ContainerBase 的invoke方法如下:

  1. protected ?Pipeline?pipeline?=? new ?StandardPipeline( this ); //標準管道的實現StandardPipeline ??
  2. public ? void ?invoke(Request?request,?Response?response)??
  3. ??????? throws ?IOException,?ServletException?{??
  4. ???????pipeline.invoke(request,?response); //調用管道里的invoke ??
  5. ???}??

由代碼可知 ContainerBase 的invoke方法是傳遞到Pipeline,調用了Pipeline的invoke方法。這里要說一下Pipeline這個類,這是一個管道類,每一個管道類 Pipeline 包含數個閥類,閥類是實現了Valve接口的類,Valve接口聲明了invoke方法。管道和閥的概念跟servlet編程里面的過濾器機制非常像, 管道就像過濾器鏈,閥就好比是過濾器 。不過管道中還有一個基礎閥的概念,所謂基礎閥就是在管道中當管道把所有的普通閥都調用完成后再調用的。不管是普通閥還是基礎閥,都實現了Value接口,也都繼承于抽象類ValveBase。在tomcat中,當調用了管道的invoke方法,管道則會順序調用它里面的閥的invoke方法。先看看管道StandardPipeline的invoke方法:

  1. public ? void ?invoke(Request?request,?Response?response)??
  2. ???????? throws ?IOException,?ServletException?{??
  3. ???????? //?Invoke?the?first?Valve?in?this?pipeline?for?this?request ??
  4. ????????( new ?StandardPipelineValveContext()).invokeNext(request,?response);??
  5. ????}??

其中StandardPipelineValveContext是管道里的一個內部類,內部類的作用是幫助管道順序調用閥Value的invoke方法,下面看它的定義代碼:

  1. protected ? class ?StandardPipelineValveContext??
  2. ????? implements ?ValveContext?{??
  3. ????? protected ? int ?stage?=? 0 ;??
  4. ????? public ?String?getInfo()?{??
  5. ????????? return ?info;??
  6. ?????}??
  7. ????? public ? void ?invokeNext(Request?request,?Response?response)??
  8. ????????? throws ?IOException,?ServletException?{??
  9. ????????? int ?subscript?=?stage; //閥的訪問變量 ??
  10. ?????????stage?=?stage?+? 1 ; //當前訪問到第幾個閥 ??
  11. ????????? //?Invoke?the?requested?Valve?for?the?current?request?thread ??
  12. ????????? if ?(subscript?<?valves.length)?{??
  13. ?????????????valves[subscript].invoke(request,?response,? this ); //管道的閥數組 ??
  14. ?????????}? else ? if ?((subscript?==?valves.length)?&&?(basic?!=? null ))?{??
  15. ?????????????basic.invoke(request,?response,? this ); //當基礎閥調用完成后,調用管道的基礎閥的invoke閥 ??
  16. ?????????}? else ?{??
  17. ????????????? throw ? new ?ServletException??
  18. ?????????????????(sm.getString( "standardPipeline.noValve" ));??
  19. ?????????}??
  20. ?????}??
  21. ?}??
內部類 StandardPipelineValveContext的invokeNext方法通過使用局部變量來訪問下一個管道數組,管道類的變量stage保存當前訪問到第幾個閥,valves保存管道的所有閥, 在調用普通閥的invoke方法是,會把內部類 StandardPipelineValveContext本身傳進去,這樣在普通閥中就能調用invokeNext方法以便訪問下一個閥的invoke方法 ,下面 看一個普通閥的invoke方法:

  1. public ? void ?invoke(Request?request,?Response?response,?ValveContext?valveContext)??
  2. ?? throws ?IOException,?ServletException?{??
  3. ?? //?Pass?this?request?on?to?the?next?valve?in?our?pipeline ??
  4. ??valveContext.invokeNext(request,?response); //使用調用下一個閥的invoke方法 ??
  5. ??System.out.println( "Client?IP?Logger?Valve" );??
  6. ??ServletRequest?sreq?=?request.getRequest();??
  7. ??System.out.println(sreq.getRemoteAddr());??
  8. ??System.out.println( "------------------------------------" );??
  9. }??
這個閥的invoke方法,通過傳進來到 StandardPipelineValveContext(實現了ValveContext接口 )的invokeNext方法來實現調用下一個閥的invoke方法。然后簡單的打印了請求的ip地址。

最后再看 StandardPipelineValveContext的invokeNext方法,調用完普通閥數組valves的閥后,開始調用基礎閥basic的 invoke方法,這里先說基礎閥的初始化,在每一個容器的構造函數類就已經初始化了基礎閥,看容器StandardEngine的構造函數:

  1. public ?StandardEngine()?{??
  2. ??????? super ();??
  3. ???????pipeline.setBasic( new ?StandardEngineValve()); //容器StandardEngine的基礎閥StandardEngineValve ??
  4. ???}??
即在容器構造的時候就已經把基礎閥添加進管道pipeline中,這樣在 StandardPipelineValveContext中的invokeNext方法里就能調用 基礎閥的invoke了,當basic.invoke(request, response, this);進入基礎閥StandardEngineValve,看基礎閥 StandardEngineValve的invoke方法:

  1. public ? void ?invoke(Request?request,?Response?response,??
  2. ????????????????????ValveContext?valveContext)??
  3. ????? throws ?IOException,?ServletException?{??
  4. ????...........................??
  5. ??
  6. ????? //?Ask?this?Host?to?process?this?request ??
  7. ?????host.invoke(request,?response);??
  8. ??
  9. ?}??

這里省略了很多代碼,主要是為了更加理解調用邏輯,在StandardEngine的基礎閥StandardEngineValve里,調用了子容器invoke方法(這里子容器就是StandardHost), 還記得一開始connector.invoke(request, response)(即StandardEngine的invoke方法)現在順利的傳遞到子容器StandardHost的invoke方法,變成了StandardHost.invoke(request, response)。 由此可以猜測StandardHost也會傳遞給它的子容器,最后傳遞到最小的容器StandardWrapper的invoke方法,然后調用StandardWrapper的基礎閥StandardWrapperValue的invoke方法,由于StandardWrapper是最小的容器了,不能再傳遞到其他容器的invoke方法了,那它的invoke方法做了什么?主要做了兩件事, 1:創建一個過濾器鏈并 ?2:分配一個servlet或者jsp,主要代碼如下:

  1. StandardWrapperValue的invoke方法??
  2. ????????????servlet?=?wrapper.allocate();?? //分配一個servlet ??
  3. ....................................................................??
  4. ????????? //?Create?the?filter?chain?for?this?request ??
  5. ????????????ApplicationFilterChain?filterChain?=??
  6. ????????????createFilterChain(request,?servlet);??
  7. .........................................................??
  8. ????????????String?jspFile?=?wrapper.getJspFile(); //分配一個jsp ??
  9. ???????????? if ?(jspFile?!=? null )??
  10. ????????????????sreq.setAttribute(Globals.JSP_FILE_ATTR,?jspFile);??
  11. ???????????? else ??
  12. ????????????????sreq.removeAttribute(Globals.JSP_FILE_ATTR);??
  13. ???????????? if ?((servlet?!=? null )?&&?(filterChain?!=? null ))?{??
  14. ????????????????filterChain.doFilter(sreq,?sres); //調用過濾器鏈處理請求,sreq和sres是request和response的包裝類,在這里面會調用servlet的services方法 ??
  15. ????????????}??

這里先不關注jsp,只關注一下servlet,通過servlet = wrapper.allocate(); 進入StandardWrapper的allocate方法,allocate主要就是調用了loadServlet方法,在loadServlet方法類用tomcat自己的類加載器實例化了一個servlet對象,并調用了該servlet的init和service方法:

  1. StandardWrapper的loadServlet方法(這里省略了很多其他的代碼)??
  2. ?Servlet?servlet?=? null ;??
  3. String?actualClass?=?servletClass; //servlet的字節碼字符串 ??
  4. ?Loader?loader?=?getLoader();??
  5. ClassLoader?classLoader?=?loader.getClassLoader(); //得到類加載器 ??
  6. ?Class?classClass?=? null ;??
  7. ???????????????? if ?(classLoader?!=? null )?{??
  8. ????????????????????System.out.println( "Using?classLoader.loadClass" );??
  9. ????????????????????classClass?=?classLoader.loadClass(actualClass); //通過類加載器實例化servlet ??
  10. ????????????????}? else ?{??
  11. ????????????????????System.out.println( "Using?forName" );??
  12. ????????????????????classClass?=?Class.forName(actualClass); //通過反射實例化servlet ??
  13. ????????????????}??
  14. ?servlet?=?(Servlet)?classClass.newInstance(); //實例化servlet ??
  15. ??servlet.init(facade); //調用servlet的init ??
  16. ????? if ?((loadOnStartup?>? 0 )?&&?(jspFile?!=? null ))?{??
  17. ???????????????????? //?Invoking?jspInit ??
  18. ????????????????????HttpRequestBase?req?=? new ?HttpRequestBase();??
  19. ????????????????????HttpResponseBase?res?=? new ?HttpResponseBase();??
  20. ????????????????????req.setServletPath(jspFile);??
  21. ????????????????????req.setQueryString( "jsp_precompile=true" );??
  22. ????????????????????servlet.service(req,?res);??
  23. ????????????????}; //調用jsp的service方法,jsp會被編譯成servlet,所以也會有service方法 ??

至此已經把請求傳遞到servlet的service(或者jsp的service)方法,整個處理請求到這里就結束了,剩下的就是返回客戶端了。這里提出幾個問題。 1:那么多的servlet,tomcat是怎么知道要請求到哪個servlet? 這個問題留待下篇博客再來講吧。

Tomcat源碼分析(四)--容器處理鏈接之責任鏈模式


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 鄂伦春自治旗| 焦作市| 淄博市| 银川市| 宁蒗| 东光县| 武冈市| 巫溪县| 忻州市| 牙克石市| 惠州市| 安顺市| 乡城县| 中宁县| 磐安县| 七台河市| 新巴尔虎右旗| 新竹市| 许昌市| 射阳县| 嘉义县| 霍林郭勒市| 灯塔市| 盱眙县| 拉萨市| 建昌县| 宁都县| 措勤县| 巴里| 宁津县| 修文县| 黑山县| 平泉县| 邮箱| 军事| 通道| 乐业县| 临清市| 大洼县| 敦煌市| 江门市|