ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Filter过滤器及其作用

2021-12-29 18:58:28  阅读:163  来源: 互联网

标签:filter 及其 req public Filter void 过滤器 resp


1.什么是过滤器?

过滤器,顾名思义就是起到过滤筛选作用的一种事物,只不过相较于现实生活中的过滤器,这里的过滤器过滤的对象是客户端访问的web资源,也可以理解为一种预处理手段,对资源进行拦截后,将其中我们认为的杂质(用户自己定义的)过滤,符合条件的放行,不符合的则拦截下来

2.filter配置

  •  web.xml配置

<filter>
     <filter-name>filter1</filter-name>//定义的filter名字
     <filter-class>work.filter.Filter1</filter-class>//filter类名(加上包名)
 </filter>
 ​
 <filter-mapping>
     <filter-name>filter1</filter-name>
     <url-pattern>/*</url-pattern>//拦截的路径(一般使用/*,当然也可以根据自己的需要来自行配置)
 </filter-mapping>
  • 使用注解配置

@WebFilter(filterName = "Filterone",urlPatterns = "/*")

也可以简写为

@WebFilter("/*")

2.filter过滤器生命周期及其与生命周期相关的方法


Filter接口有三个方法,并且这个三个都是与Filter的生命相关的方法

destory():代表是filter销毁方法 当filter对象销毁时执行该方法

doFilter:代表filter执行过滤的核心方法,如果某资源在已经被配置到这个filter进行过滤的话,那么每次访问这个资源都会执行doFilter方法

init:代表filter对象初始化方法 filter对象创建时执行

public class Filterone implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

过滤器可以组成一个过滤器链,链中的每个过滤器都可以负责特定的操作和任务,客户端访问服务器的请求和响应在这些过滤器链之间传递,Filter接口用于调用过滤器中的一系列过滤器

3.过滤器的实例应用

3.1.解决乱码问题

配置过滤器在客户发送请求到达Servlet之前进行拦截后通过拦截器直接解决中文乱码问题

实现步骤:

  • 定义拦截器实现Filter接口
  • 配置拦截器(推荐使用注解配置,简单快捷)
  • 在doFilter()中设计乱码的解决代码
@WebFilter("/*")
public class FilterEnocding implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
       resp.setContentType("text/html;charset=UTF-8");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

3.2.敏感词过滤

  • 对请求的数据进行敏感词汇过滤
  • 过滤敏感词替换为*

3.3.登录验证

  • 访问资源,验证是否已经登录
  • 如果已经登录直接放行
  • 如果未登录就挑战到登陆页面

登录selevet判断登录

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("name");
        String password = req.getParameter("pwd");
        method me = new method();
        userinfo u = me.login(username,password);
        if (u!=null){
            req.getSession().setAttribute("u",u);
            resp.sendRedirect("show");
//            req.getRequestDispatcher("show").forward(req,resp);
        }else {
            resp.sendRedirect("orr.jsp");
        }

    }

进入主页面查询展示信息selevet,代码不再详细展示

        req.getRequestDispatcher("/fil/show.jsp").forward(req,resp);

过滤器配置以及过滤器内容

 

@WebFilter("/fil/*")//过滤器配置路径
public class Filterlogin implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        userinfo u = (userinfo) req.getAttribute("u");
        if(u!=null){
            chain.doFilter(req, resp);//登录成功则继续向下执行进入展示页面
        }else {
            response.sendRedirect("../index.jsp");//失败则返回登录页面继续登录
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

标签:filter,及其,req,public,Filter,void,过滤器,resp
来源: https://blog.csdn.net/qq_52986363/article/details/122221048

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有