后台获取 HttpServletResponse 中的值
1:具体步骤 写个类继承HttpServletResponseWrapper 在类里 输出respopnse中的值
2: 在过滤器哪里引用-----之后在将数据写入到response中
类的写法
package org.gocom.components.coframe.auth.intercepter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class DatasWrapper extends HttpServletResponseWrapper {
private class ResponsePrintWriter extends PrintWriter
{
ByteArrayOutputStream output;
public ResponsePrintWriter(ByteArrayOutputStream output)
{
super(output);
this.output = output;
}
public ByteArrayOutputStream getByteArrayOutputStream()
{
return output;
}
}
private ResponsePrintWriter writer;
private ByteArrayOutputStream output;
public DatasWrapper(HttpServletResponse httpServletResponse)
{
super(httpServletResponse);
output = new ByteArrayOutputStream();
writer = new ResponsePrintWriter(output);
}
public void finalize() throws Throwable
{
super.finalize();
output.close();
writer.close();
}
public String getContent()
{
try
{
writer.flush();
return writer.getByteArrayOutputStream().toString("GBK");
}
catch(UnsupportedEncodingException e)
{
return "UnsupportedEncoding";
}
}
public void close() throws IOException
{
writer.close();
}
public PrintWriter getWriter() throws IOException
{
return writer;
}
}
过滤器的写法 , 顺序 在 chain 之前创建继承类wrapperResponse , 将wrapperResponse 传入chain ,之后就可以用wrapperResponse 获取想要得值----最后在将值写入到reponse中
HttpServletResponse httpRep = (HttpServletResponse) response; // 传进的response
DatasWrapper wrapperResponse = new DatasWrapper(httpRep); //封装response
chain.doIntercept(httpReq, wrapperResponse); // 在 chain的时候将封装的wrapperResponse传入
String html = wrapperResponse.getContent(); // 在chain 后 获取response 中得值
response.setContentLength(-1);
response.getWriter().write( html); // 将值重新写入到 response 这里得response 是传入过滤器中,不是封装得wrapperResponse