一个历史项目,客户要求改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); } } }
返回栏目页:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
编写具体的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。
作者:csdn博客 yueritian
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索servlet
, string
, java filter
, import
, javax
, public
ioException
j2ee filter、kotlin替换java写j2ee、privoxy filter 替换、subs filter 替换不了、j2ee 页面框架,以便于您获取更多的相关知识。