筆記

          way

          javax.servlet.Filter接口定義了接口的生命周期,該接口定義了三個方法:
          1 init(FilterConfig config) :容器加載filter的時候觸發(fā)此方法,可以用來從web.xml文件收集初始化參數(shù)以及做些初始化操作。FilterConfig可用來獲得初始化數(shù)據(jù),此方法被調用一次且僅一次。
          2 destroy():當Filter被從內存中unload的時候調用(最典型的是關閉應用的時候),該方法最好用來關閉Filter初始化的外部資源。
          3 doFilter(ServletRequest request, ServletResponse response, FilterChain chain) :執(zhí)行filter要完成的事情,HttpServlet情況下, ServletRequestServletResponse 對象是HttpServletRequest 和HttpServletResponse的實例。Filter 需要類型轉換他們以便使用HttpServletRequest 和HttpServletResponse對象的方法。
          FilterConfig 對象用來配置filter,FilterChain 對象用來表示filters鏈中當前應用到request和response中的filter。Filter 和Servlet的功能基本類似,唯一區(qū)別時候他強制分開鏈中的各個資源??梢哉fFilter是有額外功能的Servlet。FilterChain對象可以形象的理解為一種Stack,statck中的某個filter正在某個request和response上運行。FilterConfigServletConfig 極為類似
             默認情況filters鏈只能用來處理客戶端發(fā)送的request,如果用RequestDispatcher 對象的forward() 或者include() 方法發(fā)送請求,filter是不能處理的,但是可通過配置web.xml處理:dispatcher元素中有REQUEST, INCLUDE, FORWARD, ERROR四種值可用來配置。

          二 Wrapper
               定制request或者response對象的時候,只需要簡單的繼承request或response的相應wrapper類(就是對request或response進行擴展實現(xiàn))。sun公司提供四個方便的wrapper類:

          ServletRequestWrapper
          HttpServletRequestWrapper
          ServletResponseWrapper
          HttpServletResponseWrapper
          wrapper類實際上使用了Decorator模式
          posted @ 2009-05-15 10:21 yuxh 閱讀(205) | 評論 (0)編輯 收藏

          先復習下JAVA SERVLET API:
          servlet API使HTTP直接面向java平臺,這一意味著java開發(fā)人員能一種直觀的方式寫HTTP server 代碼,這種方式是對HTTP client/server通訊模式的面向對象的抽象。Servlet API有三個核心部分:servlet, request以及response 對象。A servlet is a singleton Java object whose whole purpose is to receive requests and return responses after some arbitrary back-end processing. The request object encapsulates the various details of the request, including the all-important request parameters as submitted via form fields and querystring parameters. The response object includes such key items as the response headers and the output stream that will generate the text of the response. In short, a servlet receives a request object, examines its data, does the appropriate back-end magic, and then writes and returns the response to the client.抽時間看下Servlet Specification,他規(guī)定API必須遵守的設計規(guī)則(具體實現(xiàn)是由第三方的servlet 容器提供的,容器既可以是獨立的,比如tomcat,也可以是一些更大的application server的一部份)。部署servlet之前,必須按規(guī)則打包,最基礎的打包單位是web application。Servlet Specification defines a web application as “a collection of servlets, HTML pages,classes, and other resources.” servlet容器收到請求后會決定哪一個servlet處理請求,調用該servlet的service()方法,傳給他request和response作為參數(shù)。Servlet API有三個關鍵角色:servlets,web applications, and the servlet container。一個servlet container可以擁有多個web applications。Servlet API還提供一些重要的高層功能,像session機制,他能把某一客戶的相關的請求都聯(lián)系起來(不要忘了HTTP的stateless特性),Without it, we’d be handling cookies and parsing embedded querystring session keys.(這個沒有完全理解,cookies 里是不是嵌入了查詢條件參數(shù)等?)。Servlet API直接以一種面向對象抽象的方式封裝client/sever交換模式,這樣我們就不需要親歷親為的解析HTTP請求,事實上我們收到的是一個整潔的用java封裝的request對象。說這些,最終是要強調the Servlet API is an infrastructure-level technology in the scope of modern web applications. As infrastructure, servlets provide the solid low-level foundation upon which robust web applications can be built. If you consider the routine needs of a web application, the Servlet API doesn’t attempt to provide solutions for such things. Now that we know what servlets can do,let’s look at what they leave undone. These common tasks of the domain are what a web application framework like Struts 2 will need to address.
              The execution of an interceptor can be broken into three phases: preprocessing, passing control on to the rest of the action invocation by calling invoke(), and postprocessing. Interceptors can also divert workflow by returning a control string instead of calling invoke().They also have access to all key data via the ActionInvocation instance they receive.Ultimately, interceptors can do just about anything.

          posted @ 2009-02-22 22:53 yuxh 閱讀(255) | 評論 (0)編輯 收藏
          僅列出標題
          共4頁: 上一頁 1 2 3 4 

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          收藏夾

          博客

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 辽中县| 吴堡县| 高密市| 金堂县| 胶州市| 泽州县| 三原县| 鸡泽县| 天门市| 古蔺县| 修水县| 商丘市| 余庆县| 大方县| 衡山县| 子长县| 博白县| 新安县| 临安市| 藁城市| 红桥区| 万山特区| 辰溪县| 阳春市| 长治市| 屏边| 万州区| 太湖县| 留坝县| 大安市| 旅游| 通州市| 黔西县| 景宁| 新宁县| 芒康县| 兴安盟| 哈尔滨市| 新昌县| 佛冈县| 龙门县|