J2EE 通过Filter替换返回页面的内容

一个历史项目,客户要求改ip,由于系统较为久远,没有源代码,通过反编译后发现ip写死在类中,反编译后的代码基本没法进行修改,分析了代码后,发现与ip相关的内容为返回到页面中的字符串,所以,想到了使用Filter,在Filter中处理这些字符串,觉得可行,便查询了一些资料,试验成功。以下这个类是从网上找到的,试验成功后发现页面已经关了,对原作者表示抱歉的同时表示感谢。

package org.mice.ipchange;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

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

public class ResponseWrapper extends HttpServletResponseWrapper {
	private ByteArrayOutputStream buffer = null;
	private ServletOutputStream out = null;
	private PrintWriter writer = null;

	public ResponseWrapper(HttpServletResponse resp) throws IOException {
		super(resp);
		buffer = new ByteArrayOutputStream();// 真正存储数据的流
		out = new WapperedOutputStream(buffer);
		writer = new PrintWriter(new OutputStreamWriter(buffer,
				this.getCharacterEncoding()));
	}

	// 重载父类获取outputstream的方法
	public ServletOutputStream getOutputStream() throws IOException {
		return out;
	}

	// 重载父类获取writer的方法
	public PrintWriter getWriter() throws UnsupportedEncodingException {
		return writer;
	}

	// 重载父类获取flushBuffer的方法
	public void flushBuffer() throws IOException {
		if (out != null) {
			out.flush();
		}
		if (writer != null) {
			writer.flush();
		}
	}

	public void reset() {
		buffer.reset();
	}

	public byte[] getResponseData() throws IOException {
		// 将out、writer中的数据强制输出到WapperedResponse的buffer里面,否则取不到数据
		flushBuffer();
		return buffer.toByteArray();
	}
	
	 //内部类,对ServletOutputStream进行包装  
    private class WapperedOutputStream extends ServletOutputStream{  
        private ByteArrayOutputStream bos=null;  
        public WapperedOutputStream(ByteArrayOutputStream stream) throws IOException{  
            bos=stream;  
        }  
        public void write(int b) throws IOException{  
            bos.write(b);  
        }  
    }  
}

编写具体的Filter,内容如下

package org.mice.ipchange;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IPChangeFilter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// 需要设置这个,以防乱码
		response.setContentType("text/html;charset=GBK");

		HttpServletRequest httpReq = (HttpServletRequest) request;
		HttpServletResponse httpResp = (HttpServletResponse) response;

		ResponseWrapper respWrapper = new ResponseWrapper(httpResp);

		chain.doFilter(request, respWrapper);
		// weblogic8不支持response.setCharacterEncoding
		// response.setCharacterEncoding("GBK");
		
		String content = new String(respWrapper.getResponseData());
		String result = replaceText(content);
		httpResp.setContentType("text/html;charset=GBK");
		PrintWriter out = httpResp.getWriter();
		out.println(result);
		out.close();
	}

	/**
	 * 替换内容
	 */
	private String replaceText(String content) {
		Enumeration enumes = RESOURCE_BUNDLE.getKeys();
		while (enumes.hasMoreElements()) {
			String o = enumes.nextElement().toString();
			content = content.replaceAll(o, RESOURCE_BUNDLE.getString(o));
		}
		return content;
	}
	// 替换文字的配置文件路径
	private static final String BUNDLE_NAME = "org.mice.ipchange.ipchange";
	// 加载配置文件
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
			.getBundle(BUNDLE_NAME);
	
	// 获取替换文字
	public static String getString(String key) {
		try {
			return RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			e.printStackTrace();
		}
		return key;
	}
	
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}


	public void destroy() {
		// TODO Auto-generated method stub
	}
}
最后,在配置文件ipchange.properties中配置好你要替换的键值对就可以了,对了,别忘了在你的web.xml中定义Filter,并使用Filter。


你可能感兴趣的:(J2EE 通过Filter替换返回页面的内容)