测试一个filter的过滤字符代码,,从网上参考的。。
public class FileCaptureResponseWrapper extends HttpServletResponseWrapper { private CharArrayWriter output; public String toString(){
return output.toString();
} public FileCaptureResponseWrapper(HttpServletResponse response){
super(response);
output=new CharArrayWriter();
} public PrintWriter getWriter(){
return new PrintWriter(output);
}
}public class WordFilter implements Filter{
protected FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException{
this.filterConfig=config;
} public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws
IOException,ServletException{
PrintWriter out=response.getWriter();
FileCaptureResponseWrapper wrapper=new FileCaptureResponseWrapper((HttpServletResponse)response);
chain.doFilter(request,wrapper);
String resStr=wrapper.toString();
String newStr=""; if(resStr.indexOf("is")>0){
newStr=resStr.replaceAll("is","***");
}
else{
newStr=resStr;
} out.println(newStr);
} public void destroy(){
this.filterConfig=null;
} public void setFilterConfig(final FilterConfig filterConfig){
this.filterConfig=filterConfig;
}
}
运行后,代码一直抱错
getWriter() has already been called for this response,
输出页面也是乱的。。请教,为什么?应该如何修改??谢谢
public class FileCaptureResponseWrapper extends HttpServletResponseWrapper { private CharArrayWriter output; public String toString(){
return output.toString();
} public FileCaptureResponseWrapper(HttpServletResponse response){
super(response);
output=new CharArrayWriter();
} public PrintWriter getWriter(){
return new PrintWriter(output);
}
}public class WordFilter implements Filter{
protected FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException{
this.filterConfig=config;
} public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws
IOException,ServletException{
PrintWriter out=response.getWriter();
FileCaptureResponseWrapper wrapper=new FileCaptureResponseWrapper((HttpServletResponse)response);
chain.doFilter(request,wrapper);
String resStr=wrapper.toString();
String newStr=""; if(resStr.indexOf("is")>0){
newStr=resStr.replaceAll("is","***");
}
else{
newStr=resStr;
} out.println(newStr);
} public void destroy(){
this.filterConfig=null;
} public void setFilterConfig(final FilterConfig filterConfig){
this.filterConfig=filterConfig;
}
}
运行后,代码一直抱错
getWriter() has already been called for this response,
输出页面也是乱的。。请教,为什么?应该如何修改??谢谢
解决方案 »
- 用户下载文件时文件名的显示问题
- 页面弹出窗口并锁定父窗口
- 我在系统上自动获取了一个时间,现在我想在这个基础上加30天得到另外一个日期怎么加啊
- 用myeclipse建jsf的最简单例子的问题,新手高分请教
- 相同的程序在Tomcat上没有中文问题,移植到Resin上就出现了中文问题?该怎么解决?是不是JSP程序没有办法在不同的应用平台上运行。
- 14k javascript 写的超级玛丽请大家多多指导!!
- 我写了个.java文件,编译是出现以下错误,请哪位大虾指教~~~急急急急~~~~~~~~
- 奇怪的.java.policy.請幫忙看看
- Sun ONE Studio VS jbuilder
- 关于图片显示的,帮帮忙
- Microsoft][SQLServer 2000 Driver for JDBC]ResultSet can not re-read row data for column 3.
- 请教 resin 出现java.lang.OutOfMemoryError怎么解决?
return new PrintWriter(output);
}
这个方法不要行不?
return new PrintWriter(output);
}也一样报错呢。
你用的是什么服务器,是servlet的哪一个规范,我好象记得servlet2.3以前好象只能调用一次getWriter方法,但是2。3好象可以重复调用了。你是不是用的是老规范但是重复调用了多次getWriter,在jsp里面最好不要自己调用,因为jsp生成servlet时容器生成的代码里面已经调用过一次了,不然jsp里面也不会有out对象
你打开了,而且没有out.close
但是就算你close了,jsp好像也不能输出了