zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0

          ?

          Struts 源碼的切片學習(三)

          —— ActionServlet 的初始化細節


          ?

          Author ???? :????? zhyiwww

          E-Mail ????? :????? zhyiwww@163.com

          Date ????????? :????? 2007-1-10

          轉載請注明出處 www.aygfsteel.com/zhyiwww

          (copyright by @ zhangyi)

          ?

          ?

          ActionServlet 初始化的過程中,有一個函數 initServlet() 用來初始化此 ActionServlet, 主要是初始化其 url, 也就是到底此 ActionServlet 接收和處理那些 url

          ?

          我們先看一下代碼:

          protected void initServlet() throws ServletException {

          ?

          ??????? // Remember our servlet name

          ??????? this.servletName = getServletConfig().getServletName();

          ?

          ??????? // Prepare a Digester to scan the web application deployment descriptor

          ?????? ?Digester digester = new Digester();

          ?????? ? digester.push(this);

          ??????? digester.setNamespaceAware(true);

          ??????? digester.setValidating(false);

          ?

          ??????? // Register our local copy of the DTDs that we can find

          ??????? for (int i = 0; i < registrations.length; i += 2) {

          ??????????? URL url = this.getClass().getResource(registrations[i+1]);

          ??????????? if (url != null) {

          ??????????????? digester.register(registrations[i], url.toString());

          ??????????? }

          ?????? ?}

          ?????????? /********************************************

          ???????????????? 上面的紫色的代碼是把當前的 ActionServlet 實例放入到解析堆棧中。這一步很重要。

          ???????????????? 正是 actionServlet 放入了堆棧,下面的調用所取得配置參數才正是 actionSerlvet 的配置參數,而不是其他的 servlet 的配置參數。

          ?????????? 這個地方是一個難點。

          ????? ????? ********************************************/

          ????????????????????

          ?

          ??????? // Configure the processing rules that we need

          ???? ???digester.addCallMethod("web-app/servlet-mapping",

          ?????????????????????????????? "addServletMapping", 2);

          ??????? digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);

          ??????? digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);

          ????????????????????

          ????????????? /************************************************************

          ???????????????????? 上面的代碼執行的時候, digester 就會調用 actionServlet 中的 addServletMapping() 方法,并傳進去兩個參數。

          ???????????????????? 在這個方法里面,就初始化了 servlet url, ,也就是那種后綴的 ur 要由 actionServlet 來接收和處理。

          ???????????????????? 例如: *.do 還是 /action/*.do 等。

          ????????????? 這個 servlet url 保存在 actionServlet servletMapping 參數里面。 ?????????

          ????????????? ************************************************************/

          ?

          ??????? // Process the web application deployment descriptor

          ??????? if (log.isDebugEnabled()) {

          ??????????? log.debug("Scanning web.xml for controller servlet mapping");

          ??????? }

          ?

          ??? ????InputStream input =

          ??????????? getServletContext().getResourceAsStream("/WEB-INF/web.xml");

          ?

          ??????? if (input == null) {

          ??????????? log.error(internal.getMessage("configWebXml"));

          ??????????? throw new ServletException(internal.getMessage("configWebXml"));

          ??????? }

          ?

          ??????? try {

          ??????????? digester.parse(input);

          ?

          ??????? } catch (IOException e) {

          ??????????? log.error(internal.getMessage("configWebXml"), e);

          ??????????? throw new ServletException(e);

          ?

          ??????? } catch (SAXException e) {

          ??????????? log.error(internal.getMessage("configWebXml"), e);

          ??????????? throw new ServletException(e);

          ?

          ??????? } finally {

          ??????????? try {

          ??????????????? input.close();

          ??????????? } catch (IOException e) {

          ??????????????? log.error(internal.getMessage("configWebXml"), e);

          ??????????????? throw new ServletException(e);

          ??????????? }

          ??????? }

          ?

          ??????? // Record a servlet context attribute (if appropriate)

          ??????? if (log.isDebugEnabled()) {

          ??????????? log.debug("Mapping for servlet '" + servletName + "' = '" +

          ??????????????? servletMapping + "'");

          ???? ???}

          ?

          ??????? if (servletMapping != null) {

          ???????? ???getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);

          ??????? }

          ?

          }

          ?

          上面的過程就是 initServlet 的初始化過程,其中的難點就是對 digester.push() 的理解。

          ??? 附:

          ??? public void addServletMapping(String servletName, String urlPattern) {

          ??????? if (log.isDebugEnabled()) {
          ??????????? log.debug("Process servletName=" + servletName +
          ????????????????????? ", urlPattern=" + urlPattern);
          ??????? }
          ??????? if (servletName == null) {
          ??????????? return;
          ??????? }
          ??????? if (servletName.equals(this.servletName)) {
          ??????????? this.servletMapping = urlPattern;
          ??????? }

          ??? }





          |----------------------------------------------------------------------------------------|
                                     版權聲明  版權所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2007-01-29 17:18 zhyiwww 閱讀(2066) 評論(1)  編輯  收藏 所屬分類: j2ee

          FeedBack:
          # re: Struts源碼的切片學習(三)-- ActionServlet的初始化細節
          2008-03-03 15:56 | gff
          這個寫的還不錯.  回復  更多評論
            
          主站蜘蛛池模板: 宣武区| 肇州县| 霞浦县| 西峡县| 汉阴县| 大冶市| 天水市| 论坛| 平陆县| 巫山县| 开阳县| 毕节市| 台湾省| 哈巴河县| 稻城县| 富顺县| 封丘县| 牡丹江市| 南皮县| 德江县| 沅江市| 虎林市| 特克斯县| 佛冈县| 宝丰县| 乌苏市| 色达县| 定陶县| 朝阳区| 蒙城县| 梓潼县| 乌苏市| 克什克腾旗| 竹溪县| 错那县| 平安县| 陇南市| 汽车| 博客| 凌源市| 长治市|