HttpServletRequestWrapper simulates distributed sessions

Source: Internet
Author: User

HttpServletRequestWrapper simulates distributed sessions

The content of HttpSession is put in a separate Map to simulate a remote distributed Session.

1. Use HttpServletRequestWrapper to create a custom Request
2. Use Dynamic proxy to wrap the http session object returned by the custom Request
3. Create a filter and replace the original Request object with a custom Request.
4. the http Session object obtained in the Servlet is written and read through the remote Session server.

Creates a custom Request and returns the http session of the dynamic proxy.


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 {

// Simulate a remote Session server. The Key indicates the SessionId and the Value indicates the Session content.

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 ("[saved] 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 ("[retrieve] key:" + args [0] + ", value:" + result );

Return result;

}

Return method. invoke (session, args );

}

 

}
Use a filter to replace the original 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

 

}

}

Use HttpSession in Servlet in the original way.


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

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

Session. getAttribute ("name ");

Session. getAttribute ("other ");

}
The result shows that he has simulated data access from a remote server.

[Save] key: name, value: Hello
[Retrieve] key: name, value: Hello
[Retrieve] key: other, value: null

HBase 0.94.21 + ZooKeeper-3.4.6 distributed Installation

Initial construction of Zabbix distributed monitoring system in CentOS 6.0

Hadoop distributed deployment

This article permanently updates the link address:

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.