1.过滤器的作用
(1)对所有请求的内容进行统一编码,防止乱码
(2)过滤或屏蔽一些非法字符
(3)判断用户是否登录,就是判断session是否存在
2.如何编写过滤器?
(1)所在的类必须实现Filter接口(它含有三个抽象方法)
publib interface Filter{
pulbic void init(FilterConfig config) throws ServletException;
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException;
public void destroy();
}
3.常见的过滤器的写法
(1)SetCharacterEncodingFilter.java(统一编码)
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; public void init(FilterConfig config) throws ServletException { System.out.println("过滤器初始化!"); this.filterConfig = config; this.encoding = filterConfig.getInitParameter("encoding"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String encoding = this.encoding; if (encoding != null) { request.setCharacterEncoding(encoding); } else { request.setCharacterEncoding("UTF-8"); } chain.doFilter(request, response); } public void destroy() { System.out.println("过滤器销毁!"); this.encoding = null; this.filterConfig = null; } }
(2)SessionFilter.java(session是否生效)
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; //是否登陆进行过滤 public class SessionFilter implements Filter { public void init(FilterConfig config) throws ServletException { System.out.println("过滤器初始化!"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpSession session = req.getSession(); // 如果session不为空,则跳转到其它页面,否则转到登陆页面 if (session.getAttribute("user") != null) { chain.doFilter(request, response); } else { request.getRequestDispatcher("login.jsp") .forward(request, response); } } public void destroy() { System.out.println("过滤器销毁!"); } }
(3)CharFilter .java(过滤非法字符)
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; //过滤非法字符 public class CharFilter implements Filter{ public void init(FilterConfig config){ System.out.println("过滤器初始化!"); } public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{ String content=request.getParameter("content"); if(content!=null){ if(content.indexOf("<script")==-1){ chain.doFilter(request,response); }else{ System.out.println("有非法字符!"); } }else{ chain.doFilter(request, response); } } public void destroy(){ System.out.println("过滤器销毁!"); } }
另附,以上三种过滤器的web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--统一字符编码过滤器--> <filter> <filter-name>SetCharacterEncoding</filter-name> <filter-class> filter.SetCharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> <!--过滤非法字符--> <filter> <filter-name>CharFilter</filter-name> <filter-class> filter.CharFilter </filter-class> </filter> <filter-mapping> <filter-name>CharFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 是否登陆进行过滤 --> <filter> <filter-name>SessionFilter</filter-name> <filter-class> filter.SessionFilter </filter-class> </filter> <filter-mapping> <filter-name>SessionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>