使用Filter修改response

时间:2022-05-19 18:29:06

关键是要重写response和PirntWriter(或者OutputStream)

重写PirntWriter:

package com.xs.servlet.changeresponse;

import java.io.PrintWriter;
import java.io.Writer;

public class MyWriter extends PrintWriter{
private StringBuilder buffer;

public MyWriter(Writer out) {
super(out);
buffer = new StringBuilder();
}

@Override
public void write(char[] buf, int off, int len) {
// super.write(buf, off, len);
char[] dest = new char[len];
System.arraycopy(buf, off, dest, 0, len);
buffer.append(dest);
System.out.println("write1");
}

@Override
public void write(char[] buf) {
super.write(buf);
System.out.println("write2");
}

@Override
public void write(int c) {
super.write(c);
System.out.println("write3");
}

@Override
public void write(String s, int off, int len) {
super.write(s, off, len);
System.out.println("write4");
}

@Override
public void write(String s) {
super.write(s);
System.out.println("write5");
}

public String getContent(){
return buffer.toString();
}

}


重写OutputStream:
package com.xs.servlet.changeresponse;

import java.io.IOException;

import javax.servlet.ServletOutputStream;

/**
* 这只是示例,可以参考MyWriter
* @author Administrator
*
*/
public class MyOutputStream extends ServletOutputStream {
private ServletOutputStream outputStream;

public MyOutputStream(ServletOutputStream outputStream) {
this.outputStream = outputStream;
}

@Override
public void write(int b) throws IOException {
outputStream.write(b);
System.out.println("output1");
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
System.out.println("output2");
}

@Override
public void write(byte[] b) throws IOException {
super.write(b);
System.out.println("output3");
}

}

重写response:

package com.xs.servlet.changeresponse;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class MyResponseWrapper extends HttpServletResponseWrapper {
private MyWriter myWriter;
private MyOutputStream myOutputStream;

public MyResponseWrapper(HttpServletResponse response) {
super(response);
}

@Override
public PrintWriter getWriter() throws IOException {
myWriter = new MyWriter(super.getWriter());
return myWriter;
}

@Override
public ServletOutputStream getOutputStream() throws IOException {
myOutputStream = new MyOutputStream(super.getOutputStream());
return myOutputStream;
}

public MyWriter getMyWriter() {
return myWriter;
}

public MyOutputStream getMyOutputStream() {
return myOutputStream;
}

}

过滤器ChangeResponseFilter:

package com.xs.servlet.changeresponse;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class ChangeResponseFilter implements Filter {

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
MyResponseWrapper responseWrapper = new MyResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, responseWrapper);
MyWriter writer = responseWrapper.getMyWriter();
if (writer != null) {
String content = writer.getContent();
content = content.replace("$replace$", "This is replaced!");
response.getWriter().write(content);
}
}

@Override
public void init(FilterConfig arg0) throws ServletException {

}

}

过滤器配置:

<filter>
<filter-name>changeResponseFilter</filter-name>
<filter-class>com.xs.servlet.changeresponse.ChangeResponseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>changeResponseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

请求资源http://localhost/test/index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test index</title>
<script type="text/javascript" src="/test/js/jquery-1.4.2.js"></script>
<script type="text/javascript">

</script>
</head>

<body>
index page.<br/>
$replace$
</body>
</html>

测试结果:

使用Filter修改response