Java Servlet Technology之过滤器(Filtering Requests and Responses)


# Query the request and act accordingly.
# Block the request-and-response pair from passing any further.
# Modify the request headers and data. You do this by providing a customized version of the request.
# Modify the response headers and data. You do this by providing a customized version of the response.
# Interact with external resources.
----------------------------------------------------------------------------------------
web.xml filter 的配置片段,在访问 /MyServlet_1.action 是进行过滤
  <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.kela.web.filters.MyFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <servlet-name>MyServlet_1</servlet-name>
  </filter-mapping>

  <servlet>
    <display-name>MyServlet_1</display-name>
    <servlet-name>MyServlet_1</servlet-name>
    <servlet-class>com.kela.web.servlets.MyServlet_1</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>MyServlet_1</servlet-name>
    <url-pattern>/MyServlet_1.action</url-pattern>
  </servlet-mapping>
---------------------------------------------------------------------------------------
MyFilter.java
public class MyFilter implements Filter {
    private FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("[INFO]: 创建... ...");
        this.filterConfig = filterConfig;
    }

    public void destroy() {
        System.out.println("[INFO]: 销毁... ...");
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                            throws IOException, ServletException {
        if (filterConfig == null)
            return;

        PrintWriter out = response.getWriter();
        CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);
        chain.doFilter(request, wrapper);
        CharArrayWriter caw = new CharArrayWriter();
        caw.write("[INFO]:扑捉的页面输出内容是 :" + wrapper.toString());

        response.setContentLength(caw.toString().getBytes().length);
        out.write(caw.toString());
        out.close();
    }

    public class CharResponseWrapper extends HttpServletResponseWrapper {
        private CharArrayWriter output;

        public String toString() {
            return output.toString();
        }

        public CharResponseWrapper(HttpServletResponse response) {
            super(response);
            output = new CharArrayWriter();
        }

        public PrintWriter getWriter() {
            return new PrintWriter(output);
        }
    }
}

-------------------------------------------------------------------------------
MyServlet_1.java 的页面输出代码片段
... ...
out.println("I love you for ever .... ....");
... ...

--------------------------------------------------------------------------------
打印输入结果:[INFO]:扑捉的页面输出内容是 :I love you for ever .... ....

你可能感兴趣的:(Java Servlet Technology之过滤器(Filtering Requests and Responses))