JSP Filter 详解

Test1988

贡献于2012-08-20

字数:0 关键词: JSP Java开发

Filter 1、 过滤器,顾问思议,就是在源数据和目的数据之间起过滤作用的中间组件。对于 Web 应用程序来说,过滤器是一个驻留在服务器端的 Web 组件,它可以截取客户 端和资源之间的请求与响应信息,并对这些信息进行过滤,如下图: 当 WEB 容器接收到一个对资源的请求时,它将判断是否有过滤器与这个资源相关 联。如果有,那么容器将请求交给过滤器进行处理。当目标资源对请求作出响应 时,容器同样会先将响应发送给过滤器。 2、 过滤器链,在一个 WEB 应用程序中,可以部署多个过滤器,这些过滤器组成了一 个过滤器链。每个过滤器负责特定的操作和任务,在请求资源时,过滤器链中的 过滤器依次对请求进行处理,并将请求传递给下一个过滤器,直达目标资源;在 发送响应是,则按照相反的顺序进行处理,直到客户端。 过滤链是按照 Web.xml 中的配置顺序生成的。 3、 例子 Filter1.java public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.write("filter1 before doFilter...." + "
"); chain.doFilter(request, response); pw.write("filter1 after doFilter...."+ "
"); } Filter2.java public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.write("filter2 before doFilter...."+ "
"); chain.doFilter(request, response); pw.write("filter2 after doFilter...."+ "
"); } Index.jsp This is my JSP page.
Web.xml filter1 filter.Filter1 filter2 filter.Filter2 filter2 /index.jsp filter1 /index.jsp OutPut: filter2 before doFilter.... filter1 before doFilter.... This is my JSP page. filter1 after doFilter.... filter2 after doFilter.... 4、 Filter API 5、 包装类中的装饰模式 6、 ServletRequestWrapper、HttpServletRequestWrapper 的实现 ServletrequestWapper.java public ServletRequestWrapper(ServletRequest request) { if (request == null) { throw new IllegalArgumentException(“Request cannot be null”); } this.request = request; } public Object getAttribute(String name) { return this.request.getAttribute(name); } HttpServletrequestWapper.java public HttpServletRequestWrapper(HttpServletRequest request) { super(request); } private HttpServletRequest _getHttpServletRequest() { return (HttpServletRequest) super.getRequest(); } public String getQueryString() { return this._getHttpServletRequest().getQueryString(); } 7、 利用包装类和 Filter 处理中文乱码 Login.jsp
UserName:
Password:

Index.jsp UserName:${param.username }
Password:${param.pwd }
MyRequestWrapper.java public class MyRequestWrapper extends HttpServletRequestWrapper{ public MyRequestWrapper(HttpServletRequest request) { super(request);//必须有此代码,否则编译器报错。 } public String getParameter(String name) { String result = super.getParameter(name); if(result != null){ try { result = new String(result.getBytes("ISO-8859-1"),"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return result; } } LoginHandler.java protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("index.jsp").forward(req, resp); } LoginFilter.java public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; chain.doFilter(new MyRequestWrapper(req), response); } Web.xml loginservlet servlet.LoginHandler loginservlet *.do loginfilter filter.LoginFilter loginfilter loginservlet

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档

相关文档