Servlet高级想法

释放双眼,带上耳机,听听看~!

filter过滤器

常用方法:

1,init方法:过滤器对象被创建以后,执行初始化方法
2,dofilter方法:实行拦截与放行
3,destroy方法:过滤器被销毁以后执行

第一个filter程序

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("测试测试");
    }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here

        // pass the request along the filter chain
        //chain.doFilter(request, response);
    }

xml文件的配置

  <filter>
    <display-name>Test_servletFilter</display-name>
    <filter-name>Test_servletFilter</filter-name>
    <filter-class>p01.filter.Test_servletFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Test_servletFilter</filter-name>
    <servlet-name>Test_servlet</servlet-name>
  </filter-mapping>

filter映射

1,使用通配符*

通配符*可以拦截所有的请求

1,完全目录匹配:/Myservlet

2,目录匹配:/aa/* 拦截aa目录下的所有请求

3,扩展名匹配:.doc .jpg 【注意不可加斜杠】

拦截不同的访问方式

加入拦截的方式,默认是拦截request

  <filter>
    <display-name>Test_servletFilter</display-name>
    <filter-name>Test_servletFilter</filter-name>
    <filter-class>p01.filter.Test_servletFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Test_servletFilter</filter-name>
    <servlet-name>Test_servlet</servlet-name>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

filter链

同一个servlet有多个过滤器的时候,过滤器的执行顺序是按照xml的排序来进行的。谁的配置在前,谁先执行

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("进入Test_servlet"+"<br/>");
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("进第一个过滤器<br/>");
        chain.doFilter(request, response);
        response.getWriter().print("出第一个过滤器<br/>");
    }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("进第二个过滤器<br/>");
        chain.doFilter(request, response);
        response.getWriter().print("出第二个过滤器<br/>");
    }
  <servlet>
    <description></description>
    <display-name>Test_servlet</display-name>
    <servlet-name>Test_servlet</servlet-name>
    <servlet-class>p01.filter.Test_servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Test_servlet</servlet-name>
    <url-pattern>/ts</url-pattern>
  </servlet-mapping>
  <filter>
    <display-name>Test_servletFilter_1</display-name>
    <filter-name>Test_servletFilter_1</filter-name>
    <filter-class>p01.filter.Test_servletFilter_1</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Test_servletFilter_1</filter-name>
    <servlet-name>Test_servlet</servlet-name>
  </filter-mapping>
  
    <filter>
    <display-name>Test_servletFilter_2</display-name>
    <filter-name>Test_servletFilter_2</filter-name>
    <filter-class>p01.filter.Test_servletFilter_2</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Test_servletFilter_2</filter-name>
    <servlet-name>Test_servlet</servlet-name>
  </filter-mapping>

人已赞赏
知识分享

WordPress提示429 Too Many Requests 完美解决方案

2020-9-17 22:22:00

知识分享

路由选择算法和分组交付

2020-9-18 22:25:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索