捕获结果的xhtml

时间:2014-10-02 11:06:05

标签: html jsf jsf-2 xhtml

我想将结果生成的HTML保存到数据库中。

我已经找到了它,但我迷路了,我不知道如何开始:S。

有人知道捕获HTML的方法吗?

THX。

1 个答案:

答案 0 :(得分:1)

我认为你可以使用ServletFilter,HttpServletResponse包装器和PrintWriter包装器。

1.create java.io.Printwriter subclass

public class PrintWriterWrapper extends PrintWriter {
    public PrintWriterWrapper(Writer out) {
        super(out);
    }
    @Override
    public void write(int c) {
        super.write(c);
        char ch = (char)c;
        System.out.println(ch); //<-please change to use DB. and others too.
    }
    @Override
    public void write(char[] buf, int off, int len) {
        super.write(buf, off, len);
        String str = new String(buf, off, len);
        System.out.println(str);
    }
    @Override
    public void write(String s, int off, int len) {
        super.write(s, off, len);
        System.out.println(s.substring(off, off+len));
    }
}

如果需要,请覆盖其他方法。

2.创建HttpServletResponse包装类

public class MyResponseWrapper extends HttpServletResponseWrapper {
    public MyResponseWrapper(HttpServletResponse response) {
        super(response);
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        PrintWriter writer = super.getWriter();
        PrintWriterWrapper wrapper = new PrintWriterWrapper(writer);
        return wrapper;
    }
}

3.创建ServletFilter并使用包装器

public class MyFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {
        MyResponseWrapper wrapper = new MyResponseWrapper((HttpServletResponse)res);
        chain.doFilter(req, wrapper);
    }
}

4.添加过滤器设置为web.xml

 <filter>
     <filter-name>MyFilter</filter-name>
     <filter-class>hoge.MyFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>MyFilter</filter-name>
     <url-pattern>/faces/*</url-pattern>
 </filter-mapping>