当前位置: 代码迷 >> JavaScript >> filter 批改jsp servlet response返回的内容
  详细解决方案

filter 批改jsp servlet response返回的内容

热度:167   发布时间:2012-09-09 09:27:54.0
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());
? }
}

  相关解决方案