映射Filter的细节一

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了映射Filter的细节一相关的知识,希望对你有一定的参考价值。

在默认情况下,Filter只过滤Request的请求,即web资源之间的转包和包含不过滤

MappingFilter::doFilter():A
FromServlet::doGet()
ToServlet::doGet()
MappingFilter::doFilter():B

FromServlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FromServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        System.out.println("FromServlet::doGet()");
        request.getRequestDispatcher("/ToServlet").forward(request,response);
        //request.getRequestDispatcher("/ToServlet").include(request,response);
    }
}

ToServlet

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ToServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        System.out.println("ToServlet::doGet()");
        //int i = 10 / 0;
    }
}

MappingFilter


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 MappingFilter implements Filter {
    public void destroy() {
    }
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
        System.out.println("MappingFilter::doFilter():A");
        chain.doFilter(request,response);
        System.out.println("MappingFilter::doFilter():B");

    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

web.xml

<filter>
    <filter-name>MappingFilter</filter-name>
    <filter-class>cn.web.filter.MappingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>MappingFilter</filter-name>
    <url-pattern>/FromServlet</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>MappingFilter</filter-name>
    <url-pattern>/ToServlet</url-pattern>
  </filter-mapping>

以上是关于映射Filter的细节一的主要内容,如果未能解决你的问题,请参考以下文章

译丨Yarn - Javascript 新一代套件管理

java8 .stream().sorted().filter().map().collect()用法

将多个输出中的hls属性设置为单独的片段代码

[转帖]龙芯下一代处理器微结构GS464E细节曝光

Filter——概述应用Filter的使用 & 细节

动态SQL基础概念复习(Javaweb作业5)