HttpServletRequestWrapper模拟实现分布式Session
2024-09-30 09:34:08
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
最新文章
- Linux脚本,关闭服务器的所有tomcat并且重新启动
- “Invalid maximum heap size” when running Maven
- 在文章没有缩略图的时候,如何去掉织梦官方的 DEDECMS无缩略图 图片
- Lucene.Net+盘古分词器(详细介绍)(转)
- web应用性能测试-Tomcat 7 连接数和线程数配置
- IDS IPS WAF之安全剖析
- 测试Swift语言代码高亮-使用highlight.js
- Install the 64bit library in Ubuntu13.10
- 欧洲用户放弃Android转投iOS原因大起底
- gdb mysq
- 分享一些前端chm文档
- main函数(本文较老,仅作参考)
- How to Tune Java Garbage Collection--reference
- mysql快速入门
- Codeforces Education Round 11
- angular 2 - 005 路由实现机制
- css---计算页面的的宽度和长度
- scss是什么?在vue.cli中的安装使用步骤是?有哪几大特性?
- java中读取配置文件的方法
- 百度地图API自动定位和3种导航
热门文章
- BZOJ 3566 [SHOI2014]概率充电器 ——期望DP
- BZOJ 4259 残缺的字符串 ——FFT
- [Vijos1308]埃及分数(迭代加深搜索 + 剪枝)
- [SCOI2005]最大子矩阵 (动态规划)
- charts 画折线图
- EC++学习笔记(四) 设计与声明
- LL(1)语法分析器 //c++实现
- C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案
- T3187 队列练习3 codevs
- MySQL主从架构配置