Kela's Blog

            前面的路很坎坷,但毕竟是条路.也许走过这一段就会发现,走过去就是梦想中的地方.因此坚持成为此刻唯一能做且必须去做的事情.
posts - 9, comments - 27, trackbacks - 0, articles - 15

# 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 .... ....

只有注册用户登录后才能发表评论。


网站导航:
 
分享到: