Httpservletrequestwrapper Simulation for distributed session

Source: Internet
Author: User

The contents of the httpsession are placed in a separate map, simulating the remote distributed session.

1. Create a custom request using Httpservletrequestwrapper
2. Wrapping the HttpSession object returned by the custom request using dynamic Proxy
3. Create a filter and replace the original request object with the custom request.
4. The HttpSession object that is obtained in the servlet, both the write and read contents are assumed to be through the remote session server.

Create a custom request that returns the httpsession of the dynamic agent

  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. Analog remote session server, key means that Sessionid,value represents the contents of the 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 ("[Deposit]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 ("[Remove]key:" + args[0] + ", Value:" + result);
  52. return result;
  53. }
  54. Return Method.invoke (session, args);
  55. }
  56. }

Replace the original request with a filter

  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. }


Use HttpSession in the servlet in the original way.

    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. }

As a result, he has simulated access to data from a remote server.

[Deposited]key:name,value:hello
[Remove]key:name,value:hello
[Remove]key:other,value:null

Httpservletrequestwrapper Simulation for distributed session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.