笔记

way

javax.servlet.Filter接口定义了接口的生命周期,该接口定义了三个方法:
1 init(FilterConfig config) :容器加载filter的时候触发此方法,可以用来从web.xml文件收集初始化参数以及做些初始化操作。FilterConfig可用来获得初始化数据,此方法被调用一次且仅一次。
2 destroy():当Filter被从内存中unload的时候调用(最典型的是关闭应用的时候),该方法最好用来关闭Filter初始化的外部资源。
3 doFilter(ServletRequest request, ServletResponse response, FilterChain chain) :执行filter要完成的事情,HttpServlet情况下, ServletRequestServletResponse 对象是HttpServletRequest 和HttpServletResponse的实例。Filter 需要类型转换他们以便使用HttpServletRequest 和HttpServletResponse对象的方法。
FilterConfig 对象用来配置filter,FilterChain 对象用来表示filters链中当前应用到request和response中的filter。Filter 和Servlet的功能基本类似,唯一区别时候他强制分开链中的各个资源。可以说Filter是有额外功能的Servlet。FilterChain对象可以形象的理解为一种Stack,statck中的某个filter正在某个request和response上运行。FilterConfigServletConfig 极为类似
   默认情况filters链只能用来处理客户端发送的request,如果用RequestDispatcher 对象的forward() 或者include() 方法发送请求,filter是不能处理的,但是可通过配置web.xml处理:dispatcher元素中有REQUEST, INCLUDE, FORWARD, ERROR四种值可用来配置。

二 Wrapper
     定制request或者response对象的时候,只需要简单的继承request或response的相应wrapper类(就是对request或response进行扩展实现)。sun公司提供四个方便的wrapper类:

ServletRequestWrapper
HttpServletRequestWrapper
ServletResponseWrapper
HttpServletResponseWrapper
wrapper类实际上使用了Decorator模式
posted @ 2009-05-15 10:21 yuxh 阅读(183) | 评论 (0)编辑 收藏

先复习下JAVA SERVLET API:
servlet API使HTTP直接面向java平台,这一意味着java开发人员能一种直观的方式写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,他规定API必须遵守的设计规则(具体实现是由第三方的servlet 容器提供的,容器既可以是独立的,比如tomcat,也可以是一些更大的application server的一部份)。部署servlet之前,必须按规则打包,最基础的打包单位是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作为参数。Servlet API有三个关键角色:servlets,web applications, and the servlet container。一个servlet container可以拥有多个web applications。Servlet API还提供一些重要的高层功能,像session机制,他能把某一客户的相关的请求都联系起来(不要忘了HTTP的stateless特性),Without it, we’d be handling cookies and parsing embedded querystring session keys.(这个没有完全理解,cookies 里是不是嵌入了查询条件参数等?)。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 阅读(236) | 评论 (0)编辑 收藏
仅列出标题
共4页: 上一页 1 2 3 4 

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

随笔分类

随笔档案

收藏夹

博客

搜索

最新评论

阅读排行榜

评论排行榜