最近在搞Filter,虽然知道有插件可以实现过滤,但是自己还是想试试接触这些比较底层的东西,想自己试试看,但是出现比较坑的问题!我的ServletResponseWrapper:
public class MyWrapper extends ServletResponseWrapper{
PrintWriter pw=null;
ByteArrayOutputStream bout=null;
WrapperOutputStream wrapper=null;
public MyWrapper(ServletResponse response) {
super(response);
bout=new ByteArrayOutputStream();
wrapper=new WrapperOutputStream(bout);
try {
pw=new PrintWriter(new OutputStreamWriter(bout,this.getCharacterEncoding()));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(byte[] b){
try {
bout.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void reset(){
bout.reset();
}
public void flushBuffer() throws IOException{
if(bout!=null)
bout.flush();
if(pw!=null)
pw.flush();
}
public byte[] getData() throws IOException{
flushBuffer();
return bout.toByteArray();
}
public ServletOutputStream getOutputStream(){
return wrapper;
}
private class WrapperOutputStream extends ServletOutputStream{
private ByteArrayOutputStream bout1=null;
public WrapperOutputStream(ByteArrayOutputStream bout1){
this.bout1=bout1;
}
@Override
public void write(int b) throws IOException {
bout1.write(b);
}
}
public PrintWriter getWriter(){
return pw;
}
}
但是我在filter中的第二句:MyWrapper wrapper=new MyWrapper((HttpServletResponse)response);chain.doFilter(request, wrapper);出现了javax.servlet.ServletException: non-HTTP request or response错误,jsp文件随便输出几行文字,我的filter是对所有的jsp文件进行过滤的,xml文件配置都没有问题!大家帮我看看哪里可能会出错,小生不胜感激!