程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> J2EE通過Filter替換返回頁面的內容

J2EE通過Filter替換返回頁面的內容

編輯:關於JAVA

一個歷史項目,客戶要求改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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved