• Tomcat过滤器filter的创建与配置


    1.过滤器对客户端发送的请求进行过滤,如果通过过滤器则chain.doFilter(request, response);向下一个过滤器传递或者进入请求资源的地方,如果请求失败则会重定向或者其他操作

     1 public class LoginFilter implements Filter {
     2 
     3     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     4             throws IOException, ServletException {
     5         // 对登录进行验证
     6         HttpServletRequest rq = (HttpServletRequest) request;
     7         HttpServletResponse rp = (HttpServletResponse) response;
     8         User userSession = (User) rq.getSession().getAttribute("userSession");
     9         if (null == userSession) {
    10             rp.sendRedirect("/news/jsp/error.jsp");
    11         } else {
    12             chain.doFilter(request, response);
    13         }
    14     }
    15 
    16     public void destroy() {
    17 
    18     }
    19 
    20     public void init(FilterConfig arg0) throws ServletException {
    21 
    22     }
    23 
    24 }
    View Code

    2.过滤器的配置

      <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>com.kgc.web.filter.LoginFilter</filter-class>//过滤器的类所在位置
      </filter>
      <filter-mapping>
        <filter-name>loginFilter</filter-name>
        <url-pattern>/jsp/admin/*</url-pattern>//设置过滤器的范围 是admin文件夹下的所有文件,只要想访问admin下的资源必须先经过该过滤器
      </filter-mapping>

     3.过滤器的范围的几种设置方式

    • 完全匹配 /index.jsp
    • 目录匹配: /admin/*
    • 扩展名匹配: *.do
    • 全部匹配: /*

    当路径对以上匹配方式都适用时范围越小优先级越高

  • 相关阅读:
    css基础
    什么是css
    js写guess网页
    JavaScript_day02
    html基础知识
    JavaScript_day01
    由一个瀑布流导出对滚动条滚动距离,可视区尺寸,元素尺寸的内容的知识点
    一个页面从输入URL到页面加载显示完成,这个过程中发生了什么?
    http常用状态吗以及分别代表什么意思?
    cookies,sessionStorage和localStorage的相同点和不同点?
  • 原文地址:https://www.cnblogs.com/MrliBlog/p/11000140.html
Copyright © 2020-2023  润新知