`

表示层模式:Intercepting Filter—拦截过滤器模式

阅读更多

Context
        The presentation-tier request handling mechanism receives many different types of
requests, which require varied types of processing. Some requests are simply forwarded to
the appropriate handler component, while other requests must be modified, audited, or
uncompressed before being further processed.
Problem
        Preprocessing and post-processing of a client Web request and response are required.
When a request enters a Web application, it often must pass several entrance tests
prior to the main processing stage. For example,
Has the client been authenticated?
Does the client have a valid session?
Is the client's IP address from a trusted network?
Does the request path violate any constraints?
What encoding does the client use to send the data?
Do we support the browser type of the client?
Some of these checks are tests, resulting in a yes or no answer that determines
whether processing will continue. Other checks manipulate the incoming data stream into a
form suitable for processing.
        The classic solution consists of a series of conditional checks, with any failed check
aborting the request. Nested if/else statements are a standard strategy, but this solution leads
to code fragility and a copy-and-paste style of programming, because the flow of the
filtering and the action of the filters is compiled into the application.
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.
Forces
        Common processing, such as checking the data-encoding scheme or logging
information about each request, completes per request.
Centralization of common logic is desired.
Services should be easy to add or remove unobtrusively without affecting existing
components, so that they can be used in a variety of combinations, such as
Logging and authentication
Debugging and transformation of output for a specific client
Uncompressing and converting encoding scheme of input
Solution
        Create pluggable filters to process common services in a standard manner without
requiring changes to core request processing code. The filters intercept incoming requests
and outgoing responses, allowing preprocessing and post-processing. We are able to add
and remove these filters unobtrusively, without requiring changes to our existing code.
We are able, in effect, to decorate our main processing with a variety of common
services, such as security, logging, debugging, and so forth. These filters are components
that are independent of the main application code, and they may be added or removed
declaratively. For example, a deployment configuration file may be modified to set up a
chain of filters. The same configuration file might include a mapping of specific URLs to
this filter chain. When a client requests a resource that matches this configured URL
mapping, the filters in the chain are each processed in order before the requested target
resource is invoked.

分享到:
评论

相关推荐

    STRUTS:filter过滤器

    通过`<url-pattern>/*</url-pattern>`,表示所有URL模式都将被此过滤器拦截。 #### 封装Request和Response 过滤器还可以通过封装`ServletRequest`和`ServletResponse`对象来改变请求和响应的行为。例如,可以使用`...

    Filter(过滤器)简介和工作原理

    **Filter(过滤器)简介** 在Java Web开发中,Filter(过滤器)是Servlet API中的一个重要组成部分,它允许开发者在请求被发送到目标资源(如Servlet、JSP页面)之前和之后进行处理。Filter可以用来实现诸如数据...

    Core j2ee Patterns Best Practices And Design Strategies

    - 表示层模式:Intercepting Filter、Front Controller、View Helper等。 - 业务层模式:Business Delegate、Value Object、Session Facade等。 - 集成层模式:Data Access Object、Service Activator等。 这些模式...

    Servlet3.0使用注解标注过滤器(Filter)

    在这个例子中,`Servlet3Filter` 类被 `@WebFilter` 注解标记,其 `filterName` 属性设置为 "Servlet3Filter",并且 `urlPatterns` 属性设置为 "/*",意味着这个过滤器将拦截所有进入应用程序的请求。 #### 六、`@...

    15_尚硅谷_Filter过滤器_王振国 - 课堂笔记1

    Filter 过滤器的工作流程图是这样的:首先,用户发送请求,Filter 过滤器拦截请求,然后判断用户是否登录,如果没有登录,则跳转到登录页面,如果已经登录,则让程序继续往下访问用户的目标资源。 在实现 Filter ...

    servlet过滤器详解

    2. **映射过滤器**:使用`<filter-mapping>`元素指定过滤器应该应用于哪些URL模式。 示例配置: ```xml <filter> <filter-name>logFilter</filter-name> <filter-class>test.filter.LogFilter</filter-class> </...

    servlet 过滤器例子 filter

    这可以通过在web.xml文件中配置`<filter-mapping>`元素来实现,指定过滤器应该拦截哪些Servlet或URL模式。 例如,下面是一个简单的过滤器配置: ```xml <filter> <filter-name>MyFilter</filter-name> <filter-...

    servlet+jsp实现过滤器 防止用户未登录访问

    url-pattern的设置决定了过滤器所要拦截的URL模式,星号(*)表示拦截所有请求。如果只想过滤特定目录或文件下的URL,可以将url-pattern设置成相应的目录或模式。过滤器的执行顺序可以根据filter-mapping的定义顺序来...

    jsp filter 过滤器判断用户是否登录

    ### JSP Filter 过滤器判断用户是否登录 在Web应用开发中,权限验证是非常重要的一个环节。通过使用过滤器(Filter),可以在请求到达目标资源(如Servlet或JSP页面)之前进行处理,例如实现登录状态检查、权限验证...

    拦截器 FILTER 样例

    ### 拦截器FILTER代码样例解析 #### 一、概述 在Web开发中,拦截器(FILTER)是一种常用的技术手段,它可以在请求被处理之前或之后执行一些操作,如用户验证、日志记录等。本文将通过一个具体的Java Filter实现...

    通过过滤器判断用户权限

    - `<filter-mapping>`:指定哪些 URL 模式会被该过滤器拦截。 #### 2. 示例 - `<filter>` 元素配置了一个名为 `userpermission` 的过滤器,并指定了其实现类为 `sports.tools.UserPermissionFilter`。 - `<filter-...

    Structs2核心过滤器

    这里定义了一个名为“struts2”的过滤器,其类名为`org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter`,这是Struts2框架提供的默认过滤器实现。 #### 2. **配置详解** - **`<filter-name>`...

    Angularjs之filter过滤器(推荐)

    AngularJS的filter过滤器是其核心特性之一,用于在数据绑定时对数据进行转换和处理。在AngularJS应用中,filter常被用来格式化数据、筛选数据或改变数据的显示方式,极大地提高了代码的可读性和用户体验。下面将详细...

    jsp filter 过滤器功能与简单用法示例.docx

    在Web应用开发中,过滤器(Filter)是一种重要的组件,它能够动态地拦截客户端请求和服务器响应,在请求到达目标资源(如Servlet或JSP页面)之前进行预处理,或者在响应返回客户端之前进行后处理。本篇文章将详细...

    jsp-14-过滤器

    在Java Web开发中,JSP(JavaServer Pages)是一种用于创建动态网页的技术,而过滤器(Filter)是Servlet规范的一部分,允许我们在请求处理前后插入逻辑,以实现诸如数据验证、字符编码转换、日志记录等功能。...

    servlet过滤器实现(Eclipse工程)

    这里的`/protected/*`表示所有以`/protected/`开头的URL都会被这个过滤器拦截。 三、过滤器生命周期方法 - `init(FilterConfig filterConfig)`: 过滤器初始化时调用,用于设置初始化参数。 - `doFilter...

    cas-client扩展拦截器支持excludes

    拦截器是软件设计模式中的一种,它在请求被实际处理之前进行拦截,可以用于权限验证、日志记录等多种用途。在CAS客户端中,拦截器常用来验证用户是否已经通过CAS服务器进行了身份验证,未认证的用户会被重定向到登录...

    过滤器机制分析和运用

    过滤器(Filter)机制是Java Web开发中一种重要的设计模式,它主要用于在请求处理之前或之后执行特定的任务,如数据校验、权限控制、字符编码转换等。在Servlet规范中,过滤器允许开发者通过实现javax.servlet....

    过滤器介绍及代码

    2. **在web.xml中注册过滤器**:在项目的部署描述符文件`web.xml`中使用`<filter>`和`<filter-mapping>`元素来配置过滤器及其拦截范围。这一步骤定义了哪些资源会被特定的过滤器拦截。 下面是一个简单的过滤器示例...

    管道过滤器

    管道过滤器(Pipe and Filter)模式是一种经典的软件设计模式,它在Java和其他许多编程语言中广泛应用,尤其是在处理数据流和事件处理系统中。这个模式的主要思想是将复杂的处理任务分解成一系列可重用的过滤器组件...

Global site tag (gtag.js) - Google Analytics