你好,游客 登录 注册 搜索
背景:
阅读新闻

HttpServletRequestWrapper模拟实现分布式Session

[日期:2014-09-14] 来源:Linux社区  作者:壹頁書 [字体:大 中 小]

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

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

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


import java.lang.reflect.InvocationHandler;
 
import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequestWrapper;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletResponseWrapper;

import javax.servlet.http.HttpSession;

 

public class RemoteSessionRequest extends HttpServletRequestWrapper {

 

    public RemoteSessionRequest(HttpServletRequest request) {

        super(request);

    }

 

    @Override

    public HttpSession getSession() {

        return RemoteSessionHandler.getInstance(super.getSession());

    }

}

 

class RemoteSessionHandler implements InvocationHandler {

    //模拟远程Session服务器,Key表示SessionId,Value表示该Session的内容

    private static Map<String, Map<String, Object>> map = new ConcurrentHashMap<String, Map<String, Object>>();

 

    private HttpSession session = null;

 

    private RemoteSessionHandler(HttpSession httpSession) {

        this.session = httpSession;

    };

 

    public static HttpSession getInstance(HttpSession httpSession) {

        InvocationHandler handler = new RemoteSessionHandler(httpSession);

        return (HttpSession) Proxy.newProxyInstance(httpSession.getClass().getClassLoader(), httpSession.getClass().getInterfaces(), handler);

    }

 

    @Override

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if ("setAttribute".equals(method.getName())) {

            String id = session.getId();

            Map<String, Object> m = map.get(id);

            if (m == null) {

                m = new HashMap<String, Object>();

                map.put(id, m);

            }

            m.put((String) args[0], args[1]);

            System.out.println("[存入]key:" + args[0] + ",value:" + args[1]);

            return null;

        } else if ("getAttribute".equals(method.getName())) {

            String id = session.getId();

            Map<String, Object> m = map.get(id);

            if (m == null) {

                return null;

            }

            Object result = m.get(args[0]);

            System.out.println("[取出]key:" + args[0] + ",value:" + result);

            return result;

        }

        return method.invoke(session, args);

    }

 

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


import java.io.IOException;
 
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.annotation.WebFilter;

import javax.servlet.http.HttpServletRequest;

 

@WebFilter("/*")

public class SessionFilter implements Filter {

    @Override

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        chain.doFilter(new RemoteSessionRequest((HttpServletRequest) request), response);

    }

 

    @Override

    public void destroy() {

        // TODO Auto-generated method stub

 

    }

 

    @Override

    public void init(FilterConfig arg0) throws ServletException {

        // TODO Auto-generated method stub

 

    }

}

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


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        HttpSession session = request.getSession();

        session.setAttribute("name", "Hello");

        session.getAttribute("name");

        session.getAttribute("other");

    }
结果可以看到,他已经模拟从远程服务器存取数据

[存入]key:name,value:Hello
 [取出]key:name,value:Hello
 [取出]key:other,value:null

HBase 0.94.21+ZooKeeper-3.4.6 分布式安装 http://www.linuxidc.com/Linux/2014-09/106534.htm

CentOS 6.0下Zabbix分布式监控系统的初步搭建 http://www.linuxidc.com/Linux/2014-08/105003.htm

Hadoop分布式部署 http://www.linuxidc.com/Linux/2014-07/104795.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/106635.htm

linux
相关资讯       HttpServletRequestWrapper  分布式Session 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款