最近在一个小项目中,出现装饰模式和代理模式。有时感觉它们很像。。好好整理了下,发现还是。。。
import java.io.IOException; import java.util.Map; import java.util.Map.Entry; 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.HttpServletRequestWrapper; public class EncodingFilter implements Filter { private String encode=null; @Override public void init(FilterConfig filterConfig) throws ServletException { //拿到配置文件中的编码值 this.encode=filterConfig.getServletContext().getInitParameter("encode"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html;charset="+encode); chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request), response); } //装饰类,用来改变不某些不想要的方法,装饰类,装饰request对象,改造和获取请求参数相关的三个方法,改造成获取有乱码的值,解决乱码后返回 // 这样一来,包装过后的request,虽然内部还是乱码,但是通过方法获取时,是解决过后没有乱码的值 class MyHttpServletRequest extends HttpServletRequestWrapper{ private HttpServletRequest request=null; private boolean EncodehasNode=true; //构造方法 public MyHttpServletRequest(HttpServletRequest request) { super(request); this.request=request; } //复写三个方法 *//** * Returns a java.util.Map of the parameters of this request. Request * parameters are extra information sent with the request. For HTTP * servlets, parameters are contained in the query string or posted form * data. * * @return an immutable java.util.Map containing parameter names as keys and * parameter values as map values. The keys in the parameter map are * of type String. The values in the parameter map are of type * String array. *//* //这三个方法是ServeltRequest接口身上的抽象方法 @Override public Map<String, String[]> getParameterMap() { try { if(request.getMethod().equals("POST")){ //--POST提交,一行代码解决乱码,返回 request.setCharacterEncoding(encode); //return getParameterMap(); return request.getParameterMap(); }else if(request.getMethod().equals("GET")){ //--GET提交获取有乱码的Map,手动编解码解决返回 Map<String, String[]> map=request.getParameterMap(); if(EncodehasNode){ for(Entry<String, String[]> entry: map.entrySet() ){ String [] vs=entry.getValue(); for(int i=0;i<vs.length;i++){ //解码 //底层解码原理: /***static char[] decode(String charsetName, byte[] ba, int off, int len) throws UnsupportedEncodingException { StringDecoder sd = deref(decoder); String csn = (charsetName == null) ? "ISO-8859-1" : charsetName; if ((sd == null) || !(csn.equals(sd.requestedCharsetName()) || csn.equals(sd.charsetName()))) { sd = null; try { Charset cs = lookupCharset(csn); if (cs != null) sd = new StringDecoder(cs, csn); } catch (IllegalCharsetNameException x) {} if (sd == null) throw new UnsupportedEncodingException(csn); set(decoder, sd); } return sd.decode(ba, off, len); } ****/ vs[i]=new String(vs[i].getBytes("iso8859-1"),encode); } } //执行完第一次后,就把它置为false,因为第一次就都编译好了 EncodehasNode=false; } return map; }else{ //其他格式,就返回 return getParameterMap(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Override public String getParameter(String name) { //调用getParameterValues得到第0位 String vs[]=getParameterValues(name); //判断是名字是不是为空 return vs ==null ? null:vs[0]; } @Override public String[] getParameterValues(String name) { return getParameterMap().get(name); } } @Override public void destroy() { } }
我们的EJB技术就是用到了代理模式。
接口:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>package bean; import javax.ejb.Remote; @Remote public interface LoginRemote { public boolean login(String id, String pwd); }</strong></span>实例类:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>import java.util.Iterator; import javax.annotation.Resource; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.sql.DataSource; import model.User; @Stateless @Remote(LoginRemote.class) public class Login implements LoginRemote { @PersistenceContext protected EntityManager em; public Login() { } @Resource(mappedName = "java:jboss/myDataSource") DataSource myDb; @Override public boolean login(String id, String pwd) { Query query = em.createQuery("select u from User u"); Iterator iter = query.getResultList().iterator(); System.out.println(iter.hasNext()); while(iter.hasNext()) { User user = (User)iter.next(); if(id.equals(user.getId()) && pwd.equals(user.getPwd())) { return true; } } return false; } }</strong></span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>import bean.Login; public class Test { public static void main(String args) { new Login().login("haha", "haha"); } }</strong></span>