this example describes how Java simply implements session to redis. Share for you for your reference, specific as follows:

in load balance, if the user access to different machines, if no session synchronization, users will be put forward, so the user experience is very bad, so we need to do session synchronization, the session reids cache server can be good To solve the problem. The following are the simple implementations of the code.

1, configuring web.xml filters:

 < filter> < filter-name> sessionFilter< /filter-name> < societies; societies; societies; societies; minorities; T; sessionFilter< /filter-name> < url-pattern> /*< /url-pattern> < /filter-mapping> 

two. The implementation of /pre>

three and SessionFilter:

 public class SessionFilter extends GenericFilterBean {private RedisTemplate. VletException {System.out.println ("filter"); HttpServletRequest re = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; TerryHttpServletRequestWrapper. Response);} public RedisTemplate getRedisTemplate () {return redisTemplate;} public void setRedisTemplate (RedisTemplate redisTemplate) {this.redisTemplate =}}}} S TerryHttpServletRequestWrapper extends HttpServletRequestWrapper {private CacheHttpSession session; private HttpServletResponse response; private RedisTemplate. (sTemplate) {super (request); this.response = response; this.redisTemplate = redisTemplate;} @Override public HttpSession getSession (Boolean) {{} {} {} = {} (); SID); session = new CacheHttpSession (null, Sid, redisTemplate); return session;} @Override public HttpSession. Mycookies.setDomain ("locahost"); mycookies.setPath ("/"); response.addCookie (mycookies);} 

five, CacheHttpSession:

 public class. E; private StringRedisSerializer stringSerializer = new StringRedisSerializer (); public CacheHttpSession (HttpSession session, String SID) {}; Public Enumeration< String> getAttributeNames () {final byte[] key = stringSerializer.serialize (SID); Object result = redisTemplate.execute. Ion {Set< byte[]> set = connection.keys (key); return set;}); if (result! = null). {Lize (b));} Enumeration< String> en = new Vector (SS).Elements (SS); return en;} return. ] v = stringSerializer.serialize ((String) value); redisTemplate.execute (New RedisCallback< Object> () {@Override public Object doInRedis);})};};} @Override public Object getAttribute (String name) {final byte[] key = stringSerializer.serialize (name); @SuppressWarnings. Throws DataAccessException {return connection.get (key);}}); return value;} @Override public String getId () {return} {}} Ssion {private HttpSession session; public HttpSessionWrapper (HttpSession session) {this.session = session;} @Override public () {} {} {} {};} De public long getLastAccessedTime () {return this.session.getLastAccessedTime ();} @Override public ServletContext getServletContext () {return this.session.getServletContext ();} {} Rval (interval);} @Override public int getMaxInactiveInterval () {return this.session.getMaxInactiveInterval ();} @Override public HttpSessionContext getSessionContext () {} {} {} Turn this.session.getAttribute (name);} @Override public Object getValue (String name) {return this.session.getValue (name);} {return this.session.getValueNames ();} @Override public void setAttribute (String name, Object value) {this.session.setAttribute (name,);} EmoveAttribute (String name) {this.session.removeAttribute (name);} @Override public void removeValue (String name) {this.session.removeValue ();} {} {} {} {} Ession.isNew ();}} 

above code can save session to redis, of course there are many problems, such as the generation of sessionId, session past, object serialization (testing to facilitate the unification of string), and so on, and so on. There is time to be perfect.

more interested in Java related content can see the site topic: "Java+MySQL database program design summary ", "Java operating skills summary" and "Java operation DOM node skills summary "

hope this article is helpful to everyone Java program design.

you may be interested in:

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Java simple implementation of session saved to redis method sample | Script Home

You may also be interested in these articles!