日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

简单的session共享的封装

發布時間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的session共享的封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目的

  session存儲在緩存服務器上(各種緩存服務器上均可,本文以memcached為例),但對開發者來說,他不用關注,只需要調用request.getSession()方法即可獲取到session,然后對session的屬性進行操作。

面臨的問題

  1. session獲取,不是從application的服務器上獲取,要從memcached上獲取。

  2. session屬性的獲取及設置,不是設置到application服務器上,而是操作memcached獲取或者設置。

解決問題的方法

  1. 使用一個HttpServletRequestWrapper的實現類,重寫getSession()方法,然后使用filter,來過濾每個請求,使request變為requestWrapper。

  2. 使用一個HttpSessionAttributeListener的實現類,重寫attributeAdded()、attributeRemoved()、attributeReplaced()方法,當屬性發生改變時需要通知memcached中的session發生改變

另外:為解決各個異構系統因語言不通可能發生的兼容問題,session以json字符串存儲。

具體代碼如下:

wrapper類

import java.io.IOException; import java.util.Map;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpSession;import org.springframework.util.StringUtils;import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.javacodegeeks.util.JacksonMapUtil; import com.javacodegeeks.util.MemcachedUtil;public class GetSessionWrapper extends HttpServletRequestWrapper{private String sessionId=null;public GetSessionWrapper(HttpServletRequest request) {super(request);}public GetSessionWrapper(HttpServletRequest request,String sessionId) {super(request);this.setSessionId(sessionId);}@Overridepublic HttpSession getSession() {HttpSession httpSession=super.getSession();//id-->sessionId;String id="davidwang456";String json=MemcachedUtil.getValue(id);if(StringUtils.isEmpty(json)){return httpSession;}httpSession.setAttribute("JPHPSESSID", id);// 讀取JSON數據Map<String, Object> userData;try {userData = JacksonMapUtil.getMapper().readValue(json, Map.class);for(Map.Entry<String, Object> entry:userData.entrySet()){ httpSession.setAttribute(entry.getKey(), entry.getValue()); } } catch (JsonParseException e) {System.out.println("json字符串不能解析成功!");} catch (JsonMappingException e) {System.out.println("json字符串不能映射到Map!");} catch (IOException e) {System.out.println("io異常!");}return httpSession;}@Overridepublic HttpSession getSession(boolean create) {HttpSession httpSession=super.getSession(create);return httpSession; }public static void main(String[] args) {String sessionId="davidwang456";String json=MemcachedUtil.getValue(sessionId);System.out.println(json);}public String getSessionId() {return sessionId;}public void setSessionId(String sessionId) {this.sessionId = sessionId;} }

?

filter類

import java.io.IOException;import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest;import org.springframework.web.util.WebUtils;public class FetchSession implements javax.servlet.Filter{private static final String regex=".*(css|html|ico|html|jpg|jpeg|png|gif|js)";@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}private static String getSessionId(ServletRequest request){HttpServletRequest httpRequest=(HttpServletRequest)request;String sessionId="";Cookie cookie =WebUtils.getCookie(httpRequest, "PHPSESSID");if(cookie!=null){return cookie.getValue();}cookie =WebUtils.getCookie(httpRequest, "JSESSIONID");if(cookie!=null){sessionId= cookie.getValue();}return sessionId;}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {String sessionId=getSessionId(request);HttpServletRequest httpRequest=(HttpServletRequest)request; String requestedUri=httpRequest.getRequestURL().toString();System.out.println(requestedUri);if(requestedUri.matches(regex)){chain.doFilter(request, response);return;}GetSessionWrapper wrapperRequest=new GetSessionWrapper(httpRequest,sessionId);//HttpSession httpSession=wrapperRequest.getSession(); chain.doFilter(wrapperRequest, response); }@Overridepublic void destroy() {} }

?

屬性監聽器

import java.io.IOException; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger;import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent;import org.springframework.util.StringUtils;import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.javacodegeeks.util.JacksonMapUtil; import com.javacodegeeks.util.MemcachedUtil;public class MySessionAttributeListener implements HttpSessionAttributeListener {private static AtomicInteger count=new AtomicInteger(0);private static AtomicInteger countU=new AtomicInteger(0);@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {int ss=count.incrementAndGet();HttpSession session=event.getSession();//String sessionId=(String) session.getAttribute("JPHPSESSID");String sessionId="davidwang456";String attributeName = event.getName();Object attributeValue = event.getValue();System.out.println("Attribute add " + attributeName + " : " + attributeValue+",ss="+ss);String json=MemcachedUtil.getValue(sessionId);if(StringUtils.isEmpty(json)){return ;}String json_new;try {json_new = attributeAddOrUpdate(json,attributeName,attributeValue);MemcachedUtil.setValue(sessionId, json_new);} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}}private String attributeAddOrUpdate(String json,String key,Object value) throws JsonParseException, JsonMappingException, IOException{ObjectMapper mapper=JacksonMapUtil.getMapper();@SuppressWarnings("unchecked")Map<String,Object> userData = mapper.readValue(json, Map.class);Boolean flag=String.class.isAssignableFrom(value.getClass());if(!flag){Map<String, Object> map = mapper.convertValue(value, Map.class);userData.putAll(map);}else{userData.put(key, value);} return mapper.writeValueAsString(userData); }private String attributeDel(String json, String key)throws JsonParseException, JsonMappingException, IOException {ObjectMapper mapper = JacksonMapUtil.getMapper();@SuppressWarnings("unchecked")Map<String, Object> userData = mapper.readValue(json, Map.class);userData.remove(key);return mapper.writeValueAsString(userData);}@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {HttpSession session=event.getSession();//String sessionId=(String) session.getAttribute("JPHPSESSID");String sessionId="davidwang456";String attributeName = event.getName();System.out.println("Attribute del : " + attributeName);String json=MemcachedUtil.getValue(sessionId);if(StringUtils.isEmpty(json)){return ;}String json_new;try {json_new = attributeDel(json,attributeName);MemcachedUtil.setValue(sessionId, json_new);} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}}@Overridepublic void attributeReplaced(HttpSessionBindingEvent event) {int ssu=countU.incrementAndGet();HttpSession session=event.getSession();//String sessionId=(String) session.getAttribute("JPHPSESSID");String sessionId="davidwang456";String attributeName = event.getName();Object attributeValue = event.getValue();System.out.println("Attribute update " + attributeName + " : " + attributeValue+",ss="+ssu);String json=MemcachedUtil.getValue(sessionId);if(StringUtils.isEmpty(json)){return ;}String json_new;try {json_new = attributeAddOrUpdate(json,attributeName,attributeValue);MemcachedUtil.setValue(sessionId, json_new);} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}}}

pom.xml依賴:

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.4.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.4.3</version></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-guava</artifactId><version>2.4.3</version></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-joda</artifactId><version>2.4.3</version></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId><version>2.4.3</version></dependency><dependency><groupId>net.spy</groupId><artifactId>spymemcached</artifactId><version>2.12.0</version></dependency>

注意:上面代碼僅為demo代碼,實際應用需重構代碼。

?

轉載于:https://www.cnblogs.com/davidwang456/p/5256874.html

總結

以上是生活随笔為你收集整理的简单的session共享的封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。