J2EE通过Filter替换返回页面的内容
当前位置:以往代写 > JAVA 教程 >J2EE通过Filter替换返回页面的内容
2019-06-14

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

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。

作者:csdn博客 yueritian

    关键字:

在线提交作业