javaEE動態代理解決亂碼問題

mytheon發表於2011-11-24

 /*昨天剛接觸到動態代理。

 *簡單的學習了一下,跟大家分享下經驗

 */

/*預備知識----去看API的文件

 *1.Class類 -- 基礎,要不學下去挺費勁...

 *2.Method類 --同上

 *3.InvocationHandler類 -- 實現介面,監聽類

 *4.Proxy類  -- 返回動態代理物件

 */

 

/*代理類

 *如果實際開發中,setCharacterEncoding不來這麼幹的...

 *只是為了練習...

 *可以把處理POST語句移動filter裡邊去

 *效率又高,程式碼又簡單...*/

package cn.mytheon.web.filter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

public class ServletRequestImpl implements InvocationHandler {

 private Object obj = null;
 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  
  //編碼處理,獲取請求方式
  if(method.getName().equals("getParameter")){
   //構建一個查詢method
   String pg = (String)obj.getClass().getMethod("getMethod", null).invoke(obj, null);
   /*請求方式是GET*/
   String result = null;
   if(pg.equals("GET")){
   //完成獲取引數,改變編碼
   result = (String)method.invoke(obj, args);
   result = new String(result.getBytes("8859_1"),"utf-8");
   return result;
   }
  
   //post方法,構造帶參method,執行setCharacterEncoding("utf-8");*/
   //構造型參
   Class[] cls = new Class[1];
   cls[0] = String.class;
   //構造實參
   Object[] arg = new Object[1];
   arg[0] = "utf-8";
   //執行語句,改變編碼
   obj.getClass().getMethod("setCharacterEncoding", cls).invoke(obj, arg);
   return method.invoke(obj, args);
  }
  return method.invoke(obj, args);
   //return "這是一個POST請求";
 }

 public ServletRequestImpl(Object object) {
  this.obj = object;
 }

}


/*過濾類

 *用Proxy返回一個代理類物件

 *把代理類物件,當做它被代理的物件傳遞到下一鏈

 *在上面的代理類中,invoke把getParameter方法捕或,把它的返回值進行了轉碼

*/

package cn.mytheon.web.filter;
import java.io.IOException;
import java.lang.reflect.Proxy;

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;


public class DemoFilter implements Filter {

 @Override
 public void destroy() {
  // TODO Auto-generated method stub

 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  Class cls = ((HttpServletRequest)request).getClass();
  
 
  ServletRequestImpl sri = new ServletRequestImpl(request);
  
  HttpServletRequest req = (HttpServletRequest)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), sri);
  
  
  System.out.println(req.getParameter("username"));
  
 
  chain.doFilter(req, response);
  
 }

 @Override
 public void init(FilterConfig arg0) throws ServletException {
  // TODO Auto-generated method stub

 }

}

 

 

相關文章