The use of dynamic proxy to solve the garbage problem sites

The use of dynamic proxy to solve the garbage problem sites

3.1 Design Patterns
software development process, encountered similar problems, the extraction model (routine) way to solve the problem of
single cases, factories, adapters, decorators, dynamic proxies

3.2 Google car scene
* _ java designed vehicle development agreement

interface ICar{ start  run  stop}
class GoogleCar implements ICar{}

* _ Hope that in the Google ecosystem Car access to the platform, enhanced auto-start function
* _ decorator pattern
scene: the secondary development time, can not get to the source, it can not be used under the premise of inheritance, to the already existing objects function into
line enhancements.
premise: You can get to all the interface is implemented decorative objects GoogleCar
realization of ideas: decorative custom-defined class implements ICar interfaces, custom decorative pass objects to be decorated

Drawbacks: If the method is implemented in the interface is too large, in the decoration method is too redundant

* _ Proxy mode dynamic
principle: to create a similar MyCar.class file in memory by virtual machines
to be created MyCar.class file tells the virtual machine:
How much should the method 1_ bytecode file is created
byte 2_ be created methods on how to implement the code

Bytecode loader:
the JDK There are some programs, specializing in the various bytecode file is loaded into memory of such a program referred to as the bytecode loader.

How to load the byte code file class file into memory?
Underlying implementation process, using the IO streaming technology, access to the data file is loaded into memory

Byte code loader: The three
system boot loader:

Case 3.3: Dynamic Proxy solve the garbage problem full stop
step
1_new DynamicWeb Project ___> Index.html

<h1>post方式提交中文</h1>
<form action="/day18_v3/ServletDemo" method="post">
	User:<input type="" name="username"/><br/>
	<input type="submit"/>
</form>

<h1>get方式提交中文</h1>
<form action="/day18_v3/ServletDemo" method="get">
	User:<input type="" name="username"/><br/>
	<input type="submit"/>
</form>

2_ServletDemo
both in the post / get method, the following statement is not Chinese garbage problem

String um=request.getParameter("username");
System.out.println(um);

3_ filter, is performed on the getParameter request () Enhanced
ideas:
judging whether the current request is the get / post request.getMethod ();
if POST, setting word: request.setCharacterEncoding ( "utf-8" ) ;, release
if it get, call the original String v = request.getParameter (name);
the v transcoding, release

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
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 EncodingFilter implements Filter {

    public EncodingFilter() {
    }

	public void destroy() {
	}
	public void init(FilterConfig fConfig) throws ServletException {
	}
	
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//将request对象转换为HttpServletRequest
		final HttpServletRequest req=(HttpServletRequest)request;
		//让JDK在内存中生成代理对象:增强了req对象上的getParameter(name);API
		HttpServletRequest myReq=(HttpServletRequest)Proxy.newProxyInstance(EncodingFilter.class.getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				
				Object obj=null;
				
				if(method.getName().equalsIgnoreCase("getParameter")){
					//获取本次请求方式
					String md=req.getMethod();
					if("get".equalsIgnoreCase(md)){
						//get方式的请求
						//调用req对象上的getParameter方法
						String v=(String)method.invoke(req, args);
						//转码
						String vv=new String(v.getBytes("iso-8859-1"),"utf-8");
						return vv;
						
					}else{
						//post方式的请求
						req.setCharacterEncoding("utf-8");
						obj=method.invoke(req, args);
					}
					
					
				}else{
					obj=method.invoke(req, args);
				}
				return obj;
			}
		});
		//打印代理对象哈希码
		System.out.println(myReq.hashCode());
		//将代理对象放行
		chain.doFilter(myReq, response);
	}
}

Guess you like

Origin blog.csdn.net/weixin_41865602/article/details/89437211