Servlet中的Filter过滤器的介绍和使用 编写过滤器
来源:互联网 发布:手机midi制作软件 编辑:程序博客网 时间:2024/06/01 09:31
在Servlet规范2.3中定义了过滤器,它能够对Servlet容器的请求和响应对象进行检查和修改。
Servlet过滤器本身并不生成请求和响应对象,只是提供过滤功能。
Servlet过滤器能够在Servlet被调用之前检查Request对象,并修改Request Header和Request内容;
在Servlet被调用之后检查Response对象,修改Response Header和Response的内容。
Servlet过滤器可以过滤的Web组件包括Servlet,JSP和HTML等文件。
Filter类似于IO中的过滤流,实现也类似于Servlet。
所有的Servlet过滤器都必须实现javax.servlet.Filter接口,并实现该接口中的三个方法:
init(FilterConfig filterConfig)
Servlet过滤器的初始化方法,Servlet容器创建Servlet过滤器实例后将调用该方法。该方法将读取web.xml文件中Servlet过滤器的初始化参数。
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
该方法完成实际的过滤操作,当客户端请求方法与过滤器设置匹配的URL时,Servlet容器将先调用过滤器的doFilter方法。FilterChain用户访问后续过滤器。
这里的ServletRequest和ServletResponse一般需要转换成具体的Servlet实现对于的对象,如:HttpServletRequest和HttpServletResponse。
destroy()
Servlet容器在销毁过滤器实例前调用该方法,在该方法中释放Servlet过滤器占用的资源。
public interface FilterA filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.
Filters are configured in the deployment descriptor of a web applicationExamples that have been identified for this design are1) Authentication Filters2) Logging and Auditing Filters3) Image conversion Filters4) Data compression Filters5) Encryption Filters6) Tokenizing Filters7) Filters that trigger resource access events8) XSL/T filters9) Mime-type chain Filter
public interface FilterChain
A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource. Filters use the FilterChain to invoke the next filter in the chain, or if the calling filter is the last filter in the chain, to invoke the resource at the end of the chain.
Filter的init方法中提供了一个FilterConfig对象,提供相关的操作:
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.itzhai.login.LoginFilter</filter-class> <init-param> <param-name>username</param-name> <param-value>arthinking</param-value> </init-param> </filter>
在init方法中获取:
@Overridepublic void init(FilterConfig filterConfig) throws ServletException { //获取Filter初始化参数 String username = filterConfig.getInitParameter("username");}
ServletContext context = filterConfig.getServletContext();
也可以在doFilter方法中根据转换好的request获取:
HttpServletRequest req = (HttpServletRequest)request;ServletContext context = req.getSession().getServletContext();
编写Filter过滤器:
public class LoginFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init LoginFilter"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //把ServletRequest和ServletResponse转换成真正的类型 HttpServletRequest req = (HttpServletRequest)request; HttpSession session = req.getSession(); //由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url String requestURI = req.getRequestURI(); if(requestURI.endsWith("login.jsp")){ chain.doFilter(request, response); return; } //判断用户是否登录,进行页面的处理 if(null == session.getAttribute("user")){ //未登录用户,重定向到登录页面 ((HttpServletResponse)response).sendRedirect("login.jsp"); return; } else { //已登录用户,允许访问 chain.doFilter(request, response); } } @Override public void destroy() { System.out.println("destroy!!!"); }}
在web.xml中配置Filter:
<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.itzhai.login.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意:一般Filter配置在所有的Servlet之前。
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //转换成实例的请求和响应对象 HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse)response; //获取评论并屏蔽关键字 String comment = req.getParameter("comment"); comment = comment.replace("A", "***"); //重新设置参数 req.setAttribute("comment", comment); //继续执行 chain.doFilter(request, response); }
Filter的执行顺序与在web.xml配置文件中的配置顺序一致,一般把Filter配置在所有的Servlet之前。
- Servlet中的Filter过滤器的介绍和使用 编写过滤器
- Java Web笔记 – Servlet中的Filter过滤器的介绍和使用 编写过滤器
- Servlet中的Filter过滤器的介绍和使用
- 【Servlet】Filter过滤器的编写和配置
- Servlet 编写Filter过滤器
- Servlet中的Filter过滤器
- Servlet中的过滤器Filter
- servlet中的Filter过滤器
- Servlet中的过滤器Filter
- Servlet和JSP页面过滤器Filter介绍
- Servlet过滤器Filter使用
- Servlet过滤器Filter使用 .
- Servlet的Filter过滤器
- Servlet的Filter过滤器
- Servlet的过滤器Filter
- servlet的filter过滤器
- 使用filter(过滤器)处理JSP和servlet的中文乱码
- javax.servlet.Filter 的过滤器的使用
- Android手机在开发调试时logcat不显示输出信息的解决办法
- How to include <android/bitmap.h> and other *.h NDK libraries to Titanium Android module?
- java Date Calendar 日期函数
- 在VS环境下以ADO方式操作Oracle数据库
- 城市三级联动
- Servlet中的Filter过滤器的介绍和使用 编写过滤器
- 解决TMS Diagram Studio箭头颜色不能变化的问题
- ARC
- 爱车过冬要注意检查 盛德奇嘉教您做好保养
- Linux下安装vmtools图文详解
- Property 'edgesForExtendedlayout' not found on object of type 解决方案
- 什么是网关?网关是否等同于路由器?
- (转)C++中extern “C”含义深层探索
- 开源语音格式speex教程(for IOS)