roy

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            1 隨筆 :: 5 文章 :: 0 評論 :: 0 Trackbacks

          Intercepting Filter(攔截過濾器?)
                  這個不用多說了,攔截用戶的請求,在WEB中實現個Filter接口,然后配置在WEB.XML下就可以用,FilterManager已經由服務器提供商寫好了,基于應用上只需實現就可以了,這里關注的是這個模式解決的問題。
                  1.客戶有沒被授權?
                  2.客戶是否具有有效會話?
                  3.客戶IP地址是否來自信任網絡段?
                  4.客戶端傳輸使用何種方法編碼?
                  5.客戶端瀏覽器類型我們支持嗎?
          所有這些將決定請求的轉發,以后后續的流程。如果不用Filter,則需要在其后的控制中做條件測試,然后再處理請求,一般用嵌套IF/ELSE就可完成。但缺點在于,
                  不夠簡練
          ,多個控制做的條件測試都相同。
                  將過濾和控制放在一起,不夠靈活,過濾通常是可變的,比如信任IP地址段,或者是過濾的條件發生變化,都會帶來對所有控制的變更。
                  靈活和簡單的解決方法就是用一種簡單的可增減組件的機制,使得每個組件完成特殊的過濾請求。
                  (原文:
                  The key to solving this problem in a flexible and unobtrusive manner is to have a simple mechanism for adding and removing processing components, in which each component completes a specific filtering action.)
                  貼個Filter

          public class StandardEncodeFilter extends BaseEncodeFilter {
          // Creates new StandardEncodeFilter
            public StandardEncodeFilter() { }
            
          public void doFilter
             (javax.servlet.ServletRequest               
              servletRequest,
              javax.servlet.ServletResponse
              servletResponse,
              javax.servlet.FilterChain
              filterChain) 
             throws java.io.IOException,
             javax.servlet.ServletException {
               String contentType 
          =
               servletRequest.getContentType();
               
          if ((contentType == null||
               contentType.equalsIgnoreCase(
               
          "application/x-www-form-urlencoded")) {
               translateParamsToAttributes
               (servletRequest,servletResponse);
               }
               filterChain.doFilter(servletRequest,
               servletResponse);
            }

            
          private void translateParamsToAttributes(
            ServletRequest request, 
            ServletResponse   response){
              Enumeration paramNames 
          = 
              request.getParameterNames();
              
          while (paramNames.hasMoreElements()) {
                String paramName 
          = (String)
                paramNames.nextElement();
                String [] values;
                values 
          = request.getParameterValues
                 (paramName);
                System.err.println(
          "paramName = "
                 
          + paramName);
                
          if (values.length == 1)
                 request.setAttribute(paramName,
                  values[
          0]);
                
          else
                 request.setAttribute(paramName, values);
              }
            }
          }
          posted on 2005-09-08 14:34 風蕭蕭 閱讀(175) 評論(0)  編輯  收藏 所屬分類: J2EE Patterns

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 郓城县| 温泉县| 塔河县| 察隅县| 苍溪县| 扬州市| 神农架林区| 马山县| 芒康县| 凤山市| 阜新市| 丽水市| 铁岭市| 乌拉特前旗| 香港| 西藏| 元阳县| 永寿县| 平和县| 精河县| 武鸣县| 金坛市| 九龙城区| 西乡县| 乐山市| 漠河县| 敦煌市| 尉氏县| 呈贡县| 黄骅市| 昂仁县| 宁河县| 抚州市| 陕西省| 淳安县| 五华县| 壤塘县| 清镇市| 道孚县| 加查县| 梁河县|