標籤:
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
HttpServletRequestWrapper類比實現分布式Session