HttpServletRequestWrapper模拟实现分布式Session

HttpSession的内容都放在一个单独的Map中,模拟远程分布式Session。

1.使用HttpServletRequestWrapper创建自定义Request
2.使用动态代理包装自定义Request返回的HttpSession对象
3.创建过滤器,使用自定义Request替换原有的Request对象。
4.在Servlet中得到的HttpSession对象,写入和读取内容都假设通过远程Session服务器。

创建自定义的Request,返回动态代理的HttpSession

  1. import java.lang.reflect.InvocationHandler;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Proxy;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.concurrent.ConcurrentHashMap;

  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletRequestWrapper;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpServletResponseWrapper;
  11. import javax.servlet.http.HttpSession;

  12. public class RemoteSessionRequest extends HttpServletRequestWrapper {

  13.     public RemoteSessionRequest(HttpServletRequest request) {
  14.         super(request);
  15.     }

  16.     @Override
  17.     public HttpSession getSession() {
  18.         return RemoteSessionHandler.getInstance(super.getSession());
  19.     }
  20. }

  21. class RemoteSessionHandler implements InvocationHandler {
  22.     //模拟远程Session服务器,Key表示SessionId,Value表示该Session的内容
  23.     private static Map<String, Map<String, Object>> map = new ConcurrentHashMap<String, Map<String, Object>>();

  24.     private HttpSession session = null;

  25.     private RemoteSessionHandler(HttpSession httpSession) {
  26.         this.session = httpSession;
  27.     };

  28.     public static HttpSession getInstance(HttpSession httpSession) {
  29.         InvocationHandler handler = new RemoteSessionHandler(httpSession);
  30.         return (HttpSession) Proxy.newProxyInstance(httpSession.getClass().getClassLoader(), httpSession.getClass().getInterfaces(), handler);
  31.     }

  32.     @Override
  33.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  34.         if ("setAttribute".equals(method.getName())) {
  35.             String id = session.getId();
  36.             Map<String, Object> m = map.get(id);
  37.             if (== null) {
  38.                 m = new HashMap<String, Object>();
  39.                 map.put(id, m);
  40.             }
  41.             m.put((String) args[0], args[1]);
  42.             System.out.println("[存入]key:" + args[0] + ",value:" + args[1]);
  43.             return null;
  44.         } else if ("getAttribute".equals(method.getName())) {
  45.             String id = session.getId();
  46.             Map<String, Object> m = map.get(id);
  47.             if (== null) {
  48.                 return null;
  49.             }
  50.             Object result = m.get(args[0]);
  51.             System.out.println("[取出]key:" + args[0] + ",value:" + result);
  52.             return result;
  53.         }
  54.         return method.invoke(session, args);
  55.     }

  56. }
使用过滤器替换原有的Request

  1. import java.io.IOException;
  2. import javax.servlet.Filter;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. import javax.servlet.annotation.WebFilter;
  9. import javax.servlet.http.HttpServletRequest;

  10. @WebFilter("/*")
  11. public class SessionFilter implements Filter {
  12.     @Override
  13.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  14.         chain.doFilter(new RemoteSessionRequest((HttpServletRequest) request), response);
  15.     }

  16.     @Override
  17.     public void destroy() {
  18.         // TODO Auto-generated method stub

  19.     }

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

  23.     }
  24. }

在Servlet中按照原有方式使用HttpSession。

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2.         HttpSession session = request.getSession();
  3.         session.setAttribute("name", "Hello");
  4.         session.getAttribute("name");
  5.         session.getAttribute("other");
  6.     }
结果可以看到,他已经模拟从远程服务器存取数据

[存入]key:name,value:Hello
[取出]key:name,value:Hello
[取出]key:other,value:null
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值