过滤器Filter
Posted wuxu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过滤器Filter相关的知识,希望对你有一定的参考价值。
1.filter的简介
filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目 标资源访问前后进行逻辑处理
2.快速入门
步骤:
1)编写一个过滤器的类实现Filter接口
2)实现接口中尚未实现的方法(着重实现doFilter方法)
3)在web.xml中进行配置(主要是配置要对哪些资源进行过滤)
3.Filter的API详解
(1)filter生命周期及其与生命周期相关的方法
Filter接口有三个方法,并且这个三个都是与Filter的生命相关的方法
init(Filterconfig):代表filter对象初始化方法 filter对象创建时执行
doFilter(ServletRequest,ServletResponse,FilterCha):代表filter执行过滤的核心方法,如果某资源在已经被配置到这个filter进行过滤的话,那么每次访问这个资源都会执行doFilter方法
destory():代表是filter销毁方法 当filter对象销毁时执行该方法
Filter对象的生命周期:
Filter何时创建:服务器启动时就创建该filter对象
Filter何时销毁:服务器关闭时filter销毁
(2)Filter的AP详解
1)init(FilterConfig)
其中参数config代表 该Filter对象的配置信息的对象,内部封装是该filter的配置信息。
2)destory()方法
filter对象销毁时执行
3)doFilter方法
doFilter(ServletRequest,ServletResponse,FilterChain)
其中的参数:
ServletRequest/ServletResponse:每次在执行doFilter方法时 web容器负责创建一个request和一个response对象作为doFilter的参数传递进来。该request个该response就是在访问目标资源的service方法时的request和response。
FilterChain:过滤器链对象,通过该对象的doFilter方法可以放行该请求
4.Filter的配置
url-pattern配置时
1)完全匹配 /sertvle1
2)目录匹配 /aaa/bbb/* ----最多的
/user/*:访问前台的资源进入此过滤器
/admin/*:访问后台的资源时执行此过滤器
3)扩展名匹配 *.abc *.jsp
注意:url-pattern可以使用servlet-name替代,也可以混用
dispatcher:访问的方式(了解)
REQUEST:默认值,代表直接访问某个资源时执行filter
FORWARD:转发时才执行filter
INCLUDE: 包含资源时执行filter
ERROR:发生错误时 进行跳转是执行filter
Filter的作用?
1)公共代码的提取
2)可以对request和response中的方法进行增强(装饰者模式/动态代理) 如下:实现批量处理乱码
package com.web.filter; import java.io.IOException; import java.io.UnsupportedEncodingException; 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.HttpServletRequestWrapper; public class EncodingFilter implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //request.setCharacterEncoding("UTF-8"); //在传递request之前对request的getParameter方法进行增强 /* * 装饰者模式(包装) * * 1、增强类与被增强的类要实现统一接口 * 2、在增强类中传入被增强的类 * 3、需要增强的方法重写 不需要增强的方法调用被增强对象的 * */ //被增强的对象 HttpServletRequest req = (HttpServletRequest) request; //增强对象 EnhanceRequest enhanceRequest = new EnhanceRequest(req); chain.doFilter(enhanceRequest, response); } @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { } } class EnhanceRequest extends HttpServletRequestWrapper{ private HttpServletRequest request; public EnhanceRequest(HttpServletRequest request) { super(request); this.request = request; } //对getParaameter增强 @Override public String getParameter(String name) { String parameter = request.getParameter(name);//乱码 try { parameter = new String(parameter.getBytes("iso8859-1"),"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return parameter; } }
package com.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class EncodingServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String parameter = request.getParameter("username");//中文 System.out.println(parameter); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3)进行权限控制 如下:记住密码自动登陆
package com.web.filter; import java.io.IOException; import java.net.URLDecoder; import java.sql.SQLException; 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.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.ithiema.domain.User; import com.ithiema.service.UserService; public class AutoLoginFilter implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; HttpSession session = req.getSession(); //获得cookie中用户名和密码 进行登录的操作 //定义cookie_username String cookie_username = null; //定义cookie_password String cookie_password = null; //获得cookie Cookie[] cookies = req.getCookies(); if(cookies!=null){ for(Cookie cookie : cookies){ //获得名字是cookie_username和cookie_password if("cookie_username".equals(cookie.getName())){ cookie_username = cookie.getValue(); //恢复中文用户名 cookie_username = URLDecoder.decode(cookie_username, "UTF-8"); } if("cookie_password".equals(cookie.getName())){ cookie_password = cookie.getValue(); } } } //判断username和password是否是null if(cookie_username!=null&&cookie_password!=null){ //登录的代码 UserService service = new UserService(); User user = null; try { user = service.login(cookie_username,cookie_password); } catch (SQLException e) { e.printStackTrace(); } //将登录的用户的user对象存到session中 session.setAttribute("user", user); } //放行 chain.doFilter(req, resp); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
package com.web.servlet; import java.io.IOException; import java.net.URLEncoder; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.ithiema.domain.User; import com.ithiema.service.UserService; public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(); //获取数据 String username = request.getParameter("username");//中文 张三 String password = request.getParameter("password"); UserService service = new UserService(); User user = null; try { user = service.login(username,password); } catch (SQLException e) { e.printStackTrace(); } if(user!=null){ //登录成功 //判断用户是否勾选自动登录 String autoLogin = request.getParameter("autoLogin"); if(autoLogin!=null){ //对中文张三进行编码 String username_code = URLEncoder.encode(username, "UTF-8");// %AE4%kfj Cookie cookie_username = new Cookie("cookie_username",username_code); Cookie cookie_password = new Cookie("cookie_password",password); //设置cookie的持久化时间 cookie_username.setMaxAge(60*60); cookie_password.setMaxAge(60*60); //设置cookie的携带路径 cookie_username.setPath(request.getContextPath()); cookie_password.setPath(request.getContextPath()); //发送cookie response.addCookie(cookie_username); response.addCookie(cookie_password); } //将登录的用户的user对象存到session中 session.setAttribute("user", user); //重定向到首页 response.sendRedirect(request.getContextPath()); }else{ //失败 转发到登录页面 提出提示信息 request.setAttribute("loginInfo", "用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
以上是关于过滤器Filter的主要内容,如果未能解决你的问题,请参考以下文章