日期:2014-05-16  浏览次数:20363 次

filter 修改jsp servlet response返回的内容

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);
}
}

?

?

?

?

public class PrePostFilter extends GenericFilter {

? public void doFilter(final ServletRequest request,
?????????????????????? final ServletResponse response,
?????????????????????? FilterChain chain)
?????? throws IOException, ServletException {
??? ? HttpServletResponse res=(HttpServletResponse)response;
??? ? CharResponseWrapper wrapper2 = new CharResponseWrapper(
??? ??? ??? ?? (HttpServletResponse)response);
??? ? chain.doFilter(request, wrapper2);
??? ? //System.out.println(wrapper2.toString());
??? ? ServletOutputStream out = res.getOutputStream();
??? ? out.write(wrapper2.toString().getBytes());
? }
}