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 (m == 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 (m == 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. Linux脚本,关闭服务器的所有tomcat并且重新启动
  2. “Invalid maximum heap size” when running Maven
  3. 在文章没有缩略图的时候,如何去掉织梦官方的 DEDECMS无缩略图 图片
  4. Lucene.Net+盘古分词器(详细介绍)(转)
  5. web应用性能测试-Tomcat 7 连接数和线程数配置
  6. IDS IPS WAF之安全剖析
  7. 测试Swift语言代码高亮-使用highlight.js
  8. Install the 64bit library in Ubuntu13.10
  9. 欧洲用户放弃Android转投iOS原因大起底
  10. gdb mysq
  11. 分享一些前端chm文档
  12. main函数(本文较老,仅作参考)
  13. How to Tune Java Garbage Collection--reference
  14. mysql快速入门
  15. Codeforces Education Round 11
  16. angular 2 - 005 路由实现机制
  17. css---计算页面的的宽度和长度
  18. scss是什么?在vue.cli中的安装使用步骤是?有哪几大特性?
  19. java中读取配置文件的方法
  20. 百度地图API自动定位和3种导航

热门文章

  1. BZOJ 3566 [SHOI2014]概率充电器 ——期望DP
  2. BZOJ 4259 残缺的字符串 ——FFT
  3. [Vijos1308]埃及分数(迭代加深搜索 + 剪枝)
  4. [SCOI2005]最大子矩阵 (动态规划)
  5. charts 画折线图
  6. EC++学习笔记(四) 设计与声明
  7. LL(1)语法分析器 //c++实现
  8. C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案
  9. T3187 队列练习3 codevs
  10. MySQL主从架构配置